0

我还在接受教育,请耐心等待。:-) 我有 Teilnehmer.Java 类

@Entity
@Table(name = "Teilnehmer")
public class Teilnehmer implements java.io.Serializable {
private Integer id;
private String nachname;
private String vorname;
private String email;
@Type(type="org.domain.tkt.entity.EnumUserType",parameters={@org.hibernate.annotations.Parameter(name="type",value="org.domain.tkt.entity.Gender")})
private Gender gender;
private List<Team> teams = new ArrayList<Team>();;
private String userName;
private Rolle rolle;


public Teilnehmer() {
}

@Column(name = "user_name",unique = true,length = 16)
@Length(min = 4,max = 16)
@Pattern(regex = "^[a-zA-Z\\d_]{4,12}$",message = "{invalid_screen_name}")
@UserPrincipal
public String getUserName() {
    return this.userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

我有 Class Team.Java

@Entity
@Table(name = "Team")
public class Team implements java.io.Serializable, Comparable<Team>  {

private Integer id;
private String name;
private List<Teilnehmer> teilnehmer = new ArrayList<Teilnehmer>();;
private List<Spielelement> spielelements = new ArrayList<Spielelement>();;
private List<Turnier> turniere = new ArrayList<Turnier>();;

public Team() { }

@ManyToMany
public List<Teilnehmer> getTeilnehmer() {
    teilnehmer.size();
    return teilnehmer;
}

public void setTeilnehmer(List<Teilnehmer> teilnehmer) {
    this.teilnehmer = teilnehmer;
}

首先:我敢打赌@ManyToMany 注释存在问题,因为StackOverflow 中没有JoinColumn 等。但我的主要问题是。我有第三堂课,我尝试用他的用户名获取实际的 Teilnehmer(这是一个用户),但我会得到一个 NullPointer ..

protected Teilnehmer byUserName(String userName) {
    try {
        Query q = entityManager.createQuery("select Teilnehmer from Teilnehmer as teilnehmer inner join teilnehmer.userName as name where name like :userName");
        q.setParameter("userName", userName);
        return (Teilnehmer)q.getSingleResult();
    } catch (javax.persistence.NoResultException nre) {
        return null;
    }
}

这是在控制台中创建的 QRY Hibernate.. 第一部分看起来很棒.. 但我不知道为什么它在角色部分崩溃。在数据库中,roleId 的值为“9047”,没错。

17:33:42,562 INFO  [STDOUT] Hibernate: 
select
    teilnehmer0_.id as id7_,
    teilnehmer0_.user_name as user2_7_,
    teilnehmer0_.email as email7_,
    teilnehmer0_.nachname as nachname7_,
    teilnehmer0_.vorname as vorname7_,
    teilnehmer0_.rolle_rolle_id as rolle14_7_,
    teilnehmer0_.gender as gender7_,
    teilnehmer0_.photo as photo7_,
    teilnehmer0_.created_on as created8_7_,
    teilnehmer0_.abteilung_id as abteilung15_7_,
    teilnehmer0_.password as password7_,
    teilnehmer0_.activation_key as activation10_7_,
    teilnehmer0_.photo_image_type as photo11_7_,
    teilnehmer0_.temporary_password as temporary12_7_,
    teilnehmer0_.active as active7_ 
from
    Teilnehmer teilnehmer0_ 
where
    teilnehmer0_.user_name=?
17:33:42,595 INFO  [STDOUT] Hibernate: 
select
    rolle0_.rolle_id as rolle1_11_0_,
    rolle0_.name as name11_0_,
    rolle0_.beschreibung as beschrei3_11_0_ 
from
    rolle rolle0_ 
where
    rolle0_.rolle_id=?
17:33:42,624 FATAL [application] java.lang.NullPointerException
javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:91)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
at javax.faces.component.UICommand.broadcast(UICommand.java:383)
at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:329)
at org.ajax4jsf.component.AjaxViewRoot.broadcastEventsForPhase(AjaxViewRoot.java:304)
at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:261)
at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:474)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.domain.tkt.entity.IE9CompatablityFilter.doFilter(IE9CompatablityFilter.java:23)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)
at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.HotDeployFilter.doFilter(HotDeployFilter.java:53)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)

我不知道出了什么问题。顺便说一句:byName(String userName) 中的用户名字符串中的值是正确的,并且不为空。嗯……

至少角色的类(Rolle.Java)

@Entity
@Table(name="rolle")
public class Rolle implements java.io.Serializable {

static final long serialVersionUID=-2631452282682699312L;

private Long roleId;
private String beschreibung;
private String name;
private Set<Teilnehmer> teilnehmer;

public void setTeilnehmer(Set<Teilnehmer> teilnehmer) {
    this.teilnehmer = teilnehmer;
}

@OneToMany(mappedBy="rolle")
public Set<Teilnehmer> getTeilnehmer()
{
    return this.teilnehmer;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="rolle_id")
public Long getRoleId() {
    return this.roleId;
}

public void setRoleId(Long roleId) {
    this.roleId=roleId;
}

@Column(name="beschreibung",nullable=false,length=90)
@NotNull
@Length(max=90)
public String getBeschreibung() {
    return this.beschreibung;
}

public void setBeschreibung(String description) {
    this.beschreibung=description;
}

@Column(name="name",unique=true,nullable=false,length=30)
@NotNull
@Length(min=4,max=30)
@RoleName
@Pattern(regex="^[a-zA-Z\\d_]{4,12}$",message="{invalid_role_name}")
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name=name;
}

}

4

1 回答 1

0

您可以使用getResultList().get(0)而不是getSingleResult(). 设置byUserName()方法如下,可以解决你的问题

 protected Teilnehmer byUserName(String userName) {
   Teilnehmer t=null;
   try {
        Query q = entityManager.createQuery("select teilnehmer  from  "+Teilnehmer.class.getName() +" as teilnehmer inner join teilnehmer.userName as name where name like :userName");
        q.setParameter("userName", userName);
        t=(Teilnehmer)q.getResultList().get(0);
    } catch (javax.persistence.NoResultException nre) {

    }
   return t;
}
于 2013-10-18T16:00:30.570 回答