- This topic has 31 replies, 4 voices, and was last updated 19 years, 1 month ago by
Haris Peco.
-
AuthorPosts
-
Haris PecoMemberSEanLon11,
I make your schema local and call ME RE (without abstract classes; please try and you so)
I don’t change mapping or POJO and make test only
this is test (like your, but change for differnt mappings).This test addpublic void testForumCode() { Session session = null; Person p = new Person(); p.setName("Cosmo"); p.setSsn(new Integer(123)); p.setPid(new Integer(123)); Students stu = null; try { // Step 3 - Get a Hibernate Session // Session session = SessionManager.currentSession(); session = getSession(); // Step 4 - Persist entity to database Transaction tx = session.beginTransaction(); session.save(p); stu = new Students(); stu.setPerson(p); stu.setPid(p.getPid()); stu.setGrade("1"); System.out.println("=== " + stu.getGrade()); session.save(stu); // session.save(course); tx.commit(); System.out.println("Save successful."); } catch (HibernateException e) { e.printStackTrace(); System.out.println("First Attempt failed."); } finally { try { // Step 5 - close the session if (session != null) session.close(); } catch (HibernateException e1) { // do nothing } } }
HibernateUtil is simple java class (from hibernate CaveatEmptor)
If you want I can send you generated *.hbm.xml and POJO filesBest
SEanLon11MemberIn all actuality, that will not work b/c I do not have a “setPid()” for Students. However, I added that method, and it gave me the exact same error. I’m a little confused…..did you get this little function to work on your machine?? I would like to see the files that you did use.
One thing I never posted was my hibernate.cfg.xml file, which is posted here:
<?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"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="myeclipse.connection.profile">Many</property> <property name="connection.url">jdbc:postgresql://localhost/inh</property> <property name="connection.username">postgres</property> <property name="connection.password">postgres</property> <property name="connection.driver_class">org.postgresql.Driver</property> <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property><mapping resource="sessionclasses/Courses.hbm.xml" /><mapping resource="sessionclasses/Person.hbm.xml" /><mapping resource="sessionclasses/Profesors.hbm.xml" /><mapping resource="sessionclasses/Students.hbm.xml" /><mapping resource="sessionclasses/StudentsCourses.hbm.xml" /> </session-factory> </hibernate-configuration>
Thanks for your help, and any info would help.
SEanLon11MemberIf I use session.saveOrUpdate(stu) I get a new set of errors that may be more helpful to you then me (I’m still pretty new to Hibernate):
Exception in thread "main" java.lang.ClassCastException: sessionclasses.Person at org.hibernate.type.IntegerType.set(IntegerType.java:39) at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:62) at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:44) at org.hibernate.persister.entity.BasicEntityPersister.dehydrate(BasicEntityPersister.java:1628) at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:1963) at org.hibernate.persister.entity.BasicEntityPersister.updateOrInsert(BasicEntityPersister.java:1909) at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:2149) at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:75) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86) at TEST.HibernateTest.testForumCode(HibernateTest.java:95) at TEST.HibernateTest.main(HibernateTest.java:29)
Riyad KallaMemberThis tip might help trouble shoot:
Exception in thread “main” java.lang.ClassCastException: sessionclasses.Person
at org.hibernate.type.IntegerType.set(IntegerType.java:39)Which looks like Hibernate tried to resolve an Integer value from or to the Person class that didn’t work out. Take a look at your Person class and look for all your Integer fields and see where the issue may be occuring.
SEanLon11Memberwell, I’ve updated the code to frst add the Student variable stu to the StudentSet of Person, but I get the same results:
package TEST; import java.io.OutputStream; import java.util.HashSet; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import sessionclasses.AbstractProfesors; import sessionclasses.AbstractStudents; import sessionclasses.Person; import sessionclasses.Profesors; import sessionclasses.SessionManager; import sessionclasses.Students; public class HibernateTest { public static void main(String[] args) { // Step 1 - Create a new entity Person p = new Person(); p.setName("bobby"); p.setSsn(new Integer(789)); p.setStudentsSet(new HashSet()); Students stu = null; try { //testForumCode(); // Step 3 - Get a Hibernate Session Session session = SessionManager.currentSession(); // Step 4 - Persist entity to database Transaction tx = session.beginTransaction(); session.save(p); stu = new Students(p); //stu.getPerson().setPid(23); stu.setGrade("3"); p.getStudentsSet().add(stu); output("getStus = " + p.getStudentsSet()); session.saveOrUpdate(stu); // session.save(course); tx.commit(); System.out.println("Save successful."); } catch (HibernateException e) { e.printStackTrace(); System.out.println("First Attempt failed."); } finally { try { // Step 5 - close the session SessionManager.closeSession(); } catch (HibernateException e1) { // do nothing } } } public static void output(String x) { System.out.println(x); } }
Well, I’m pretty frustrated with all this, and do not know where to go next. I am open to any suggestions, anything at all.
Thanks,
Sean
SEanLon11MemberHere is the error that the above produces:
Exception in thread "main" java.lang.ClassCastException: sessionclasses.Person at org.hibernate.type.IntegerType.set(IntegerType.java:39) at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:62) at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:44) at org.hibernate.persister.entity.BasicEntityPersister.dehydrate(BasicEntityPersister.java:1628) at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:1963) at org.hibernate.persister.entity.BasicEntityPersister.updateOrInsert(BasicEntityPersister.java:1909) at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:2149) at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:75) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86) at TEST.HibernateTest.main(HibernateTest.java:45)
Riyad KallaMemberOk so I went and downloaded postgresql, installed it on my local machine and got it all setup.
I then used your SQL script to create all the tables, hibernate-enabled my project and reverse engineered the tables, creating a session factory, using a “Sequence” ID generator and hitting Finish.
I then copied in your Main method and noticed you are using Java 5 autoboxing, so I turned that on. Now I noticed a compiler error on this line:
stu = new Students(p);
Students doesn’t have any reference or field to a Person anywhere, so I don’t know why you have this code here, so I removed the p argument and just let it create a normal student instance.
stu = new Students();
I then ran the code (I had to add Hibernate advanced libs to get the ehcache JAR into the build path for it to run) and got this exception:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.exception.SQLGrammarException: could not get next sequence value
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:96)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:85)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:481)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:476)
at com.test.Main.main(Main.java:26)
Caused by: org.postgresql.util.PSQLException: ERROR: relation “hibernate_sequence” does not exist
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1512)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1297)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:188)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:430)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:346)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:250)
at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:75)
… 9 more
First Attempt failed.Now I don’t have any experience with Post or Sequences, but I bet if you changed your PK’s to simple Integer fields that were auto increment, and set your generator to Native this would take care of this particular exception. Regardless, you never reported this exception, so it seems things are behaving quite differently on your setup and on ours.
Can you do me a favor and paste the result of MyEclipse > About > Configuration Summary for me?
SEanLon11MemberHere is the configuration summary:
*** Date: Tue Feb 28 22:44:51 CST 2006
*** System properties:
OS=WindowsXP
OS version=5.1
Java version=1.5.0_01*** MyEclipse details:
MyEclipse Enterprise WorkbenchVersion: 4.0.3 GA
Build id: 20051025-4.0.3-GA*** Eclipse details:
Eclipse SDKVersion: 3.1.1
Build id: M20050929-0840Eclipse Project SDK
Version: 3.1.1
Build id: M20050929-0840Eclipse Platform
Version: 3.1.1
Build id: M20050929-0840Eclipse Java Development Tools
Version: 3.1.1
Build id: M20050929-0840Eclipse Graphical Editing Framework
Version: 3.1
Build id: 200509301327Eclipse RCP
Version: 3.1.1
Build id: M20050929-0840Eclipse Plug-in Development Environment
Version: 3.1.1
Build id: M20050929-0840Eclipse startup command=-os
win32
-ws
win32
-arch
x86
-launcher
C:\eclipse\eclipse.exe
-name
Eclipse
-showsplash
600
-exitdata
e18_2c
-vm
C:\WINDOWS\system32\javaw.exeI am going to look into your suggestion, but my
Students stu = new Students(p)
is valid b/c if you look within the AbstractStudents, you can see that it has a constructor Student(Person p) that sets the person attributes for that particular instance of Student.
SEanLon11MemberHere is something else that I’ve tested out, that compiles and seems to save “stu” into the StudentSet within the Person class, but it does NOT enter the stu data into the database:
package TEST; import java.io.OutputStream; import java.util.HashSet; import java.util.Set; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import sessionclasses.AbstractProfesors; import sessionclasses.AbstractStudents; import sessionclasses.Person; import sessionclasses.Profesors; import sessionclasses.SessionManager; import sessionclasses.Students; public class SaveTest { public static void main(String[] args) { // Step 1 - Create a new entity Person p = new Person(); p.setName("Sean"); p.setSsn(new Integer(789)); Set stus = new HashSet(); //p.setStudentsSet(new HashSet()); Students stu = null; try { //testForumCode(); // Step 3 - Get a Hibernate Session Session session = SessionManager.currentSession(); // Step 4 - Persist entity to database Transaction tx = session.beginTransaction(); //session.save(p); stu = new Students(); //stu.getPerson().setPid(23); stu.setGrade("3"); stus.add(stu); p.setStudentsSet(stus); //p.getStudentsSet().add(stu); output("getStus = " + p.getStudentsSet()); session.save(p); // session.save(course); tx.commit(); System.out.println("Save successful."); } catch (HibernateException e) { e.printStackTrace(); System.out.println("First Attempt failed."); } finally { try { // Step 5 - close the session SessionManager.closeSession(); } catch (HibernateException e1) { // do nothing } } } }
SEanLon11MemberI tried your suggestion of adjusting everything to auto_increment, but PostgreSQL does not support this exact functionality. Instead, I converted all the PK’s to Serial, which is essentially the same thing. However, after allof that, I still continue to have the EXACT same problem.
Any other suggestions would be appreciated. Thanks
Haris PecoMemberSEanLon11,
In attach is complete project for your case – i make schema with your script, make hbm and POJO with MyEclipse rev.eng. and add test class
You can do next :
– import project in eclipse with MyEclipse
– change parameters in hibernate-postgresql.cfg.xml (YOUR…) – you have to make your schema
– run PostgresqlTest like Junit Testyou can use example for starting point and play with your schema further
Best
Attachments:
You must be logged in to view attached files.
SEanLon11MemberI still have the same error, any other suggestions???
Thanks,
Sean
Haris PecoMemberSEanLon11 ,
Do you try project ?
And suggestion : download last MyEclipse 4.1.1 – this version fix a lot problemsBest
SEanLon11MemberI have downloaded that, and it fixed the above problem. However, it now has big issues with mapping associations between Students and Courses. Are there any known bugs with that code generation?? If I need to provide you with anything else, let me know.
Thanks,
Sean
Haris PecoMemberSean,
Try MyEclipse 4.1.1, make your mapping and send us results, please
Best
-
AuthorPosts