views:

158

answers:

1

Now, i am learning hibernate, and started to using it in my project. It is a CRUD application. I used hibernate for all the crud operations. It works for all of them. But, the One-To-Many & Many-To-One, i am tired of trying it. Finally it gives me the below error.

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]

Then again i went through this video tutorial. It is very simple to me, in the beginning. But, i cant make it work. It also now, says

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]

I have ran some searches in the internet, there someone telling its a bug in Hibernate, and some says, by adding @GenereatedValue this error ll be cleared. But, nothings works for me,

I hope i ll get some fix!!

Thanks!

Here my Code:

College.java

@Entity
public class College {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int collegeId;
private String collegeName;


private List<Student> students;

@OneToMany(targetEntity=Student.class, mappedBy="college", fetch=FetchType.EAGER)
public List<Student> getStudents() {
    return students;
}
public void setStudents(List<Student> students) {
    this.students = students;
}//Other gettters & setters omitted

Student.java

@Entity
public class Student {


@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int studentId;
private String studentName;


private College college;

@ManyToOne
@JoinColumn(name="collegeId")
public College getCollege() {
    return college;
}
public void setCollege(College college) {
    this.college = college;
}//Other gettters & setters omitted

Main.java:

public class Main {

private static org.hibernate.SessionFactory sessionFactory;

  public static SessionFactory getSessionFactory() {
    if (sessionFactory == null) {
      initSessionFactory();
    }
    return sessionFactory;
  }

  private static synchronized void initSessionFactory() {
    sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

  }

  public static Session getSession() {
    return getSessionFactory().openSession();
  }

  public static void main (String[] args) {
                Session session = getSession();
        Transaction transaction = session.beginTransaction();
        College college = new College();
        college.setCollegeName("Dr.MCET");

        Student student1 = new Student();
        student1.setStudentName("Peter");

        Student student2 = new Student();
        student2.setStudentName("John");

        student1.setCollege(college);
        student2.setCollege(college);



        session.save(student1);
        session.save(student2);
        transaction.commit();
  }


}

Console:

 Exception in thread "main" org.hibernate.MappingException: Could not determine type  for: java.util.List, at table: College, for columns:  [org.hibernate.mapping.Column(students)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:290)
at org.hibernate.mapping.Property.isValid(Property.java:217)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:463)
at org.hibernate.mapping.RootClass.validate(RootClass.java:235)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1330)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1833)
at test.hibernate.Main.initSessionFactory(Main.java:22)
at test.hibernate.Main.getSessionFactory(Main.java:16)
at test.hibernate.Main.getSession(Main.java:27)
at test.hibernate.Main.main(Main.java:43)

The XML:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
<hibernate-configuration>
<session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/dummy</property>
    <property name="connection.username">root</property>
    <property name="connection.password">1234</property>
    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>
    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>

    <mapping class="test.hibernate.Student" />
    <mapping class="test.hibernate.College" />
</session-factory>

+6  A: 

You are using field access strategy (determined by @Id annotation). Put any JPA related annotation right above each field instead of getter property

@OneToMany(targetEntity=Student.class, mappedBy="college", fetch=FetchType.EAGER)
private List<Student> students;
Arthur Ronald F D Garcia
@Arthur Ronald F D Garcia: Thanks, it worked great. But, the program is now stopped by new one. `object references an unsaved transient instance - save the transient instance before flushing` Do you aware of this error. If not just leave it. I am searching.
NooBDevelopeR
@Arthur Ronald F D Garcia: Hi again, i saw solution for my current error, which i asked in my previous comment. This is the link. http://stackoverflow.com/questions/1667177/nhibernate-error-save-the-transient-instance-before-flushingThe solution is including `cascade=CascadeType.ALL`. But for me it shows error and no suggestions in my eclipse. Do you know anything about that. :)
NooBDevelopeR
@ARFDG - good one!
Tony Ennis
@Arthur +1 Good catch @MaRaVan You can use either field access strategy or property access strategy but you have to be consistent within a class hierarchy, you can't mix the strategies, at least not in JPA 1.0. JPA 2.0 makes it possible to mix strategies though, but you'd need extra annotations (and thus more complexity). So the recommendation is to pick one strategy and to stick to it in your application. See [2.2.2.2. Access type](http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e535).
Pascal Thivent
@Arthur Ronald F D Garcia: Hmmmm... I have added `persistence.cascade` Now its cleared. But i again get the old error `object references an unsaved transient instance - save the transient instance before flushing` AnY Suggestions!!! :+|
NooBDevelopeR
@Pascal: Ya, Sure!! Thx for the recommendation.
NooBDevelopeR
@All: Nobody told me about, the utter code misplacement in Main.java :(
NooBDevelopeR
@Pascal Thivent Thank you for clarify MaRaVaN
Arthur Ronald F D Garcia
@MaRaVaN Hi Maravan, did you fixed main ??? If not let know what you want
Arthur Ronald F D Garcia
@Arthur Ronald F D Garcia: Ya, i have fixed it. Thanks for asking :)
NooBDevelopeR