0

I have a POJO class with Association of Objects as below.

When I execute my main Class, getting Null pointer Exception.

Someone please help me to understand what went wrong here.

First Pojo class

@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);

Second Pojo Class

@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;

Main Class:

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();

    }

}

Stacktrace of eclipse console:

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)
6
  • I have added this many listed below Jar's,is that anything else i need to add in lib, antlr, dom4j, hibernate-commons-annotations, hibernate-core-4.2.3.Final, javaassist-3.1.4.0-ga-1.0.0-sources, javax.persistence, jboss-logging-3.1.0.GA, jta-1_1, mysql-connector-java Commented Apr 6, 2016 at 5:36
  • Please, add a list of libraries to the question. And provide versions (commons-annotations and others). And add HibernateUtil too. Commented Apr 6, 2016 at 6:36
  • Does the simple criteria query work without the fetch statement? Commented Apr 6, 2016 at 13:58
  • Criteria criteria= session.createCriteria(Club.class); List list = criteria.list(); java.lang.NullPointerException at the line of getting Criteria as list Commented Apr 8, 2016 at 6:33
  • when I debug the code it went to the Hibernate-Core-4.0.0.Final Jar has no source attached with CriteriaImpl implements org.hibernate.Criteria, java.io.Serializable Commented Apr 8, 2016 at 6:47

1 Answer 1

1

You should indicate the field, not the type in the criteria:

criteria.setFetchMode("team", FetchMode.JOIN);
Sign up to request clarification or add additional context in comments.

5 Comments

Still I am getting NPE even after changing from Team to field as team
If you declare the collection EAGER (@OneToMany(fetch = FetchType.EAGER,mappedBy = "club") and remove the setFetchMode from the criteria the list return correctly?
After changing EAGER still getting the same NPE I think it's something related to Jar to have connectivity to mySQL
it seems when i debug the code it takes me to the hibernate-core.jar in particularly QueryImpl.class before throwing NPE
Then the problem is in your app libraries, try to change your hibernate version, and post your pom.xml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.