- This topic has 9 replies, 2 voices, and was last updated 20 years ago by
Riyad Kalla.
-
AuthorPosts
-
husseinMemberI’d like to know how to use the Xdoclet plugin in MyEclipse to generate the hibernate database mappings and schema.
Riyad KallaMemberhussein,
Do you not find the integrated Hibernate support sufficient for your purposes, or you would just rather do a more hands on approach by tagging up your Java source files with XDoclet tags? If you wish to use XDoclet your self then any XDoclet/Hibernate tutorial can walk you through this process, Hibernate helps by providing autocomplete for the XDoclet tags as well as providng a means to process the files quickly. Here is the hibernate XDoclet reference: http://xdoclet.sourceforge.net/xdoclet/tags/hibernate-tags.htmlWe do not currently have a tutorial online specifically for this.
husseinMemberThank you Riyad for your help, but I’d like to tell you that i tried to use Xdoclet cause i used the mapping files that has been created by right click on the table in MyEclipse Database Explorer and click on Create Hibernate Mapping and this way work perfectly when i have just one table on my database but when i have more than one table and they have a relation when i try to run my project i got this Exception.
net.sf.hibernate.exception.SQLGrammarException: Could not save object
at net.sf.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:69)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30)
at net.sf.hibernate.impl.SessionImpl.convert(SessionImpl.java:4110)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:792)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:747)
at com.genuitec.hibernate.HibernateTest.main(HibernateTest.java:33)
Caused by: java.sql.SQLException: Table ‘messages.hibernate_unique_key’ doesn’t exist
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2847)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1531)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1622)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2376)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2297)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1860)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1705)
at net.sf.hibernate.id.TableGenerator.generate(TableGenerator.java:94)
at net.sf.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.java:59)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:774)
… 2 moreso what do you think about that.
Riyad KallaMemberCaused by: java.sql.SQLException: Table ‘messages.hibernate_unique_key’ doesn’t exist
Hard to say, but every table will need a primary key, so if your DB schema isn’t hibernate friendly this might be and issue that will either require some manual tweaking to the hbm.xml file, OR just change the DB schema to make is more hibernate friendly.
husseinMemberThank you so much for your help, but I’d like to tell you that is my database contains two tables the first one has a PK and the second one has a PK and FK for the PK in table one,so what’s the problem here?could you explain in more details what you mean by
your DB schema isn’t hibernate
and
require some manual tweaking to the hbm.xml file
Riyad KallaMemberAhh, hussein post the schema for the two tables, and the hbm.xml files that were generated, let’s have a look 😉
Be sure to wrap the contents in code blocks (code button below) so the formatting is retained.
husseinMembero.k here is the schema
Team (first table) PK team_id team_name city Player (second table) PK player_id first_name last_name draft_date salary jersey_number FK team_id
and here is the generated hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <!-- DO NOT EDIT: This is a generated file that is synchronized --> <!-- by MyEclipse Hibernate tool integration. --> <!-- Created Mon Apr 05 20:47:27 EET 2004 --> <hibernate-mapping package="com.genuitec.hibernate"> <class name="Team" table="team"> <id name="teamId" column="team_id" type="java.lang.Integer"> <generator class="hilo"/> </id> <property name="teamName" column="team_name" type="java.lang.String" not-null="true" /> <property name="city" column="city" type="java.lang.String" not-null="true" /> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <!-- DO NOT EDIT: This is a generated file that is synchronized --> <!-- by MyEclipse Hibernate tool integration. --> <!-- Created Mon Apr 05 20:47:37 EET 2004 --> <hibernate-mapping package="com.genuitec.hibernate"> <class name="Player" table="player"> <id name="playerId" column="player_id" type="java.lang.Integer"> <generator class="hilo"/> </id> <property name="firstName" column="first_name" type="java.lang.String" not-null="true" /> <property name="lastName" column="last_name" type="java.lang.String" not-null="true" /> <property name="draftDate" column="draft_date" type="java.util.Date" not-null="true" /> <property name="salary" column="salary" type="java.lang.Float" not-null="true" /> <property name="jerseyNumber" column="jersey_number" type="java.lang.Integer" not-null="true" /> <many-to-one name="team" column="team_id" class="Team" not-null="true" /> </class> </hibernate-mapping>
and here is the Exception again
net.sf.hibernate.exception.SQLGrammarException: Could not save object at net.sf.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:69) at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30) at net.sf.hibernate.impl.SessionImpl.convert(SessionImpl.java:4110) at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:792) at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:747) at com.genuitec.hibernate.HibernateTest.main(HibernateTest.java:30) Caused by: java.sql.SQLException: Table 'people.hibernate_unique_key' doesn't exist at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2847) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1531) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1622) at com.mysql.jdbc.Connection.execSQL(Connection.java:2376) at com.mysql.jdbc.Connection.execSQL(Connection.java:2297) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1860) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1705) at net.sf.hibernate.id.TableGenerator.generate(TableGenerator.java:94) at net.sf.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.java:59) at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:774) ... 2 more
Riyad KallaMemberHave you marked the PK columns at auto_increment in MySQL? If so, then you need to use the native generator, not hilo.
husseinMemberDear Riyad,
I deleted the old files and recreated them again but this time using the native generator but this time I’ve got this exception.net.sf.hibernate.exception.SQLGrammarException: could not insert: [com.genuitec.hibernate.Team] at net.sf.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:69) at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30) at net.sf.hibernate.persister.AbstractEntityPersister.convert(AbstractEntityPersister.java:1332) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:542) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:428) at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute(ScheduledIdentityInsertion.java:29) at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:941) at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:866) at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:784) at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:747) at com.genuitec.hibernate.HibernateTest.main(HibernateTest.java:33) Caused by: java.sql.SQLException: Table 'messages.team' doesn't exist at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2847) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1531) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1622) at com.mysql.jdbc.Connection.execSQL(Connection.java:2376) at com.mysql.jdbc.Connection.execSQL(Connection.java:2297) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1860) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1957) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1880) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1741) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:520) ... 7 more
I really appreciate your effort. Thanks a lot.
Riyad KallaMemberDid you change your table? “messages”, where is that comming from?
-
AuthorPosts