我有一个带对象关联的POJO类,如下所示。
当我执行我的主类时,得到Null指针异常。
有人请帮我理解这里出了什么问题。
第一个Pojo类
@Entity
@Table(name = "club")
public class Club implements Serializable{
@Id
@GeneratedValue
@Column(name = "clubId")
private Integer clubId;
@Column(name="name")
private String name;
@OneToMany(fetch = FetchType.LAZY,mappedBy = "club")
private Set<Team> team=new HashSet<Team>(0);
第二个Pojo类
@Entity
@Table(name="team")
public class Team implements Serializable{
@Id
@GeneratedValue
@Column(name="teamid")
private Integer teamId;
@Column(name="teamname")
private String teamName;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "clubid", nullable = false, insertable = false, updatable = false)
private Club club;
主类:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
public class CriteriaExecution {
public static void main(String[] args) {
Session session=HibernateUtil.getSessionFactory().openSession();
//Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Club.class);
criteria.setFetchMode("Team", FetchMode.JOIN);
List list=criteria.list();
for(int i=0;i<list.size();i++)
{
System.out.println("Values"+list.get(i));
}
// tx.commit();
session.close();
}
}
eclipse控制台的Stacktrace:
Exception in thread "main" java.lang.NullPointerException
at com.mysql.jdbc.PreparedStatement.asSql(PreparedStatement.java:649)
at com.mysql.jdbc.PreparedStatement.asSql(PreparedStatement.java:587)
at com.mysql.jdbc.PreparedStatement.toString(PreparedStatement.java:4068)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
at com.mysql.jdbc.trace.Tracer.printParameters(Tracer.aj:240)
at com.mysql.jdbc.trace.Tracer.printEntering(Tracer.aj:167)
at com.mysql.jdbc.trace.Tracer.entry(Tracer.aj:126)
at com.mysql.jdbc.trace.Tracer.ajc$before$com_mysql_jdbc_trace_Tracer$1$f51c62b8(Tracer.aj:45)
at com.mysql.jdbc.Connection.registerStatement(Connection.java)
at com.mysql.jdbc.Statement.<init>(Statement.java:270)
at com.mysql.jdbc.PreparedStatement.<init>(PreparedStatement.java:500)
at com.mysql.jdbc.Connection.clientPrepareStatement(Connection.java:2187)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4829)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4734)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:161)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:182)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:159)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1859)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1816)
at org.hibernate.loader.Loader.doQuery(Loader.java:900)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
at org.hibernate.loader.Loader.doList(Loader.java:2526)
at org.hibernate.loader.Loader.doList(Loader.java:2512)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342)
at org.hibernate.loader.Loader.list(Loader.java:2337)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:124)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1662)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
at com.user.CriteriaExecution.main(CriteriaExecution.java:47)
答案 0 :(得分:1)
您应该指明字段,而不是标准中的类型:
criteria.setFetchMode("team", FetchMode.JOIN);