Hi,
I am facing the following issue while running one to many relationship.
Basically my code includes,
1. COURSE table and
STUDENT table.
2. COURSE will be the master table containing different course names having CNO as Course Number as primary key.
3. STUDENT table contains SNO, SNAME and CNO as Student Number, Student Name and Course Number under which student is under going.
4. Following is the piece of Code.
StudentOneToMany POJOCode:
public class StudentOneToMany {
private Long studentNo;
private String studentName;
getters & setters ;
}
Course POJOCode:
public class Course {
private long courseNo;
private String courseName;
private StudentOneToMany[] studentOneToManyArray;
getters & setters;
Student.hbm.xmlCode:
<class name="edu.model.StudentOneToMany" table="STUDENTONETOMANY">
<id name="studentNo" column="STUDENTNO">
<generator class="sequence" />
</id>
<property name="studentName" column="SNAME"/>
</class>
Course.hbm.xmlCode:
<class name="edu.model.Course" table="COURSE">
<id name="courseNo" column="CNO">
<generator class="sequence" />
</id>
<property name="courseName" column="CNAME"/>
<array name="studentOneToManyArray">
<key column="CNO"/>
<list-index column="idx" />
<one-to-many class="edu.model.StudentOneToMany" />
</array>
</class>
Hibernateconfig.xmlCode:
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:SURESH</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.use_sql_comments">true</property>
<mapping resource="edu/mapping/Student.hbm.xml" />
<mapping resource="edu/mapping/StudentXtra.hbm.xml" />
<mapping resource="edu/mapping/Employee.hbm.xml" />
<mapping resource="edu/mapping/StudentOneToMany.hbm.xml" />
<mapping resource="edu/mapping/Course.hbm.xml" />
</session-factory>
Please note these two tables are not present under database Below is the Main ProgramCode:
public static void main(String args[]){
Session session = SessionUtil.currentSession();
Transaction transaction = session.beginTransaction();
StudentOneToMany studentOne = new StudentOneToMany();
StudentOneToMany studentTwo = new StudentOneToMany();
studentOne.setStudentName("abcd");
studentTwo.setStudentName("efgh");
StudentOneToMany[] studentOneToManyArray = new StudentOneToMany[] {studentOne, studentTwo};
Course course = new Course();
course.setCourseName("MCA");
course.setStudentOneToManyArray(studentOneToManyArray);
session.save(studentOneToManyArray);
session.save(course);
transaction.commit();
}
I am getting the following exception and tables are being created but data is not getting inserted into the table, please help me outCode:
[color=#FF0000]log4j:WARN No appenders could be found for logger (edu.util.SessionUtil).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.MappingException: Unknown entity: [Ledu.model.StudentOneToMany;
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:628)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1366)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:563)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:551)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
at edu.main.OneToManyTest.main(OneToManyTest.java:24)[/color]