Joined: Sat Mar 05, 2011 12:54 pm Posts: 2
|
I just started learning hibernate. I read some theory and trying to implement one example using discriminator. Please help me in resolving this issue.
person.hbm.xml: --------------- <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- column is DB-TABLE field name --> <hibernate-mapping> <class name="com.spwh.Person" table="PERSON" discriminator-value="P"> <id name="pid" column="PID"> <generator class="increment" /> </id> <discriminator column="PERSONTYPE" type="string" /> <property name="firstName" column="FIRSTNAME" type="string" /> <property name="lastName" column="LASTNAME" type="string" /> <property name="ssn" column="SSN" type="int" /> <subclass name="com.spwh.Student" discriminator-value="S"> <property name="stdId" column="STDID" type="int" /> <property name="entranceDate" column="ENTRANCEDATE" type="date" /> </subclass> <subclass name="com.spwh.Teacher" discriminator-value="T"> <property name="major" column="MAJOR" type="string" /> <property name="degree" column="DEGREE" type="string" /> </subclass> </class> </hibernate-mapping>
Person.java ----------- package com.spwh;
import java.io.Serializable;
public class Person implements Serializable { private int pid; private String firstName; private String lastName; private int ssn; private String personType;//discriminator column public Person() { } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getSsn() { return ssn; } public void setSsn(int ssn) { this.ssn = ssn; } public String getPersonType() { return personType; } public void setPersonType(String personType) { this.personType = personType; } public int getPid() { return pid; }
public void setPid(int pid) { this.pid = pid; } }
Student.java ------------- package com.spwh;
import java.util.Date;
public class Student extends Person { private int stdId; private Date entranceDate; public Student() {
} public int getStdId() { return stdId; } public void setStdId(int stdId) { this.stdId = stdId; } public Date getEntranceDate() { return entranceDate; } public void setEntranceDate(Date entranceDate) { this.entranceDate = entranceDate; } }
Teacher.java -------------- package com.spwh;
public class Teacher extends Person { private String major; private String degree; public Teacher() { } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public String getDegree() { return degree; } public void setDegree(String degree) { this.degree = degree; } }
student table data: ------------------ ID FIRST_NAME LAST_NAME 1 KIRAN G 2 PADMAJA G 3 PRAMOD V 4 HARI B 5 BALAJI G
Person table data: ----------------- PID FIRSTNAME LASTNAME SSN STDID ENTRANCEDT MAJOR DEGREE PERSONTYPE 1 Balajip Gp 123 5 0000-00-00 N MCA S 2 Kiranp Gp 456 1 0000-00-00 Y MBA T
persoan table STDID and student table ID FK relation
error when running the below main method: main() { //hibernate configuration ... //tran starts in session... Person p = (Person)session.load(Person.class, new Integer(1)); //tran comitt }
error: org.hibernate.exception.SQLGrammarException: could not load an entity: [com.spwh.Person#1] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:65) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.loader.Loader.loadEntity(Loader.java:1799) at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:93) at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:81) at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2730) at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:365) at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:346) at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:123) at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:82) at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:891) at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:849) at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:62) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98) at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:158) at com.spwh.Person$$EnhancerByCGLIB$$8db8c258.toString(<generated>) at java.lang.String.valueOf(Unknown Source) at java.io.PrintStream.println(Unknown Source) at com.spwh.ch03.PersistByHibernate.main(PersistByHibernate.java:87) Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'person0_.ENTRANCEDATE' in 'field list' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.Util.getInstance(Util.java:381) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1885) at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139) at org.hibernate.loader.Loader.getResultSet(Loader.java:1669) at org.hibernate.loader.Loader.doQuery(Loader.java:662) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224) at org.hibernate.loader.Loader.loadEntity(Loader.java:1785) ... 16 more
}
|
|