Hi all,
I've fear that this is a real newbie question, but I didnt found any answer for my problem. I have bound 2 tables via an assignment table to a many to many association.
When I insert data it works fine an the data is in the database. Obtaining data from player or team is also okay, but when I try to call the getPlayer or getTeam method i got the exception below. Is there any error in my mapping or in my database?
I created the mapping manually and generated the source by hbm2java.
I hope you posted all needed information.
Thanks for your help
cham
Hibernate version: 2.1.7
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.godyo.dartscore.hibernate.beans.Team" table="TEAM">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="uuid" type="string" unsaved-value="null">
<column name="UUID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name" column="name" type="java.lang.String"/>
<property name="description" column="description" type="java.lang.String"/>
<property name="lastModified" column="last_modified" type="java.util.Date"/>
<bag name="games" cascade="all" lazy="true" inverse="true">
<key column="teamid"/>
<one-to-many class="com.godyo.dartscore.hibernate.beans.Game"/>
</bag>
<bag name="player" table="teamplayerassignment" cascade="save-update" lazy="true">
<key column="teamid "/>
<many-to-many class="com.godyo.dartscore.hibernate.beans.Player" column="playerid"/>
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.godyo.dartscore.hibernate.beans.Player" table="PLAYER">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="uuid" type="string" unsaved-value="null">
<column name="UUID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="name" column="name" type="java.lang.String"/>
<property name="description" column="description" type="java.lang.String"/>
<property name="lastModified" column="last_modified" type="java.util.Date"/>
<bag name="teams" table="teamplayerassignment" cascade="save-update" lazy="true">
<key column="playerid "/>
<many-to-many class="com.godyo.dartscore.hibernate.beans.Team" column="teamid"/>
</bag>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Session session = HibernateUtil.currentSession();
Iterator teams = session.iterate("from com.godyo.dartscore.hibernate.beans.Team as team");
while(teams.hasNext()){
Team team = (Team) teams.next();
System.out.println("ID of Team: " + team.getUuid());
System.out.println("get Player of Team");
List player = team.getPlayer();
System.out.println("Player: " + player);
}
HibernateUtil.closeSession();
Full stack trace of any exception that occurs:Code:
Hibernate: select team0_.UUID as x0_0_ from TEAM team0_
Hibernate: select team0_.UUID as UUID0_, team0_.name as name0_, team0_.description as descript3_0_, team0_.last_modified as last_mod4_0_ from TEAM team0_ where team0_.UUID=?
Hibernate: select team0_.UUID as UUID0_, team0_.name as name0_, team0_.description as descript3_0_, team0_.last_modified as last_mod4_0_ from TEAM team0_ where team0_.UUID=?
ID of Team: 402880e50085a355010085a3594b0001
get Player of Team
Hibernate: select player0_.teamid as teamid __, player0_.playerid as playerid__, player1_.UUID as UUID0_, player1_.name as name0_, player1_.description as descript3_0_, player1_.last_modified as last_mod4_0_ from teamplayerassignment player0_ inner join PLAYER player1_ on player0_.playerid=player1_.UUID where player0_.teamid =?
- SQL Error: 1064, SQLState: 42000
- Syntax error or access violation message from server: "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '__, player0_.playerid as playerid__, player1_.UUID as UUID0_, p"
- SQL Error: 1064, SQLState: 42000
- Syntax error or access violation message from server: "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '__, player0_.playerid as playerid__, player1_.UUID as UUID0_, p"
- Failed to lazily initialize a collection
net.sf.hibernate.exception.SQLGrammarException: could not initialize collection: [com.godyo.dartscore.hibernate.beans.Team.player#402880e50085a355010085a3594b0001]
at net.sf.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:69)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30)
at net.sf.hibernate.collection.AbstractCollectionPersister.convert(AbstractCollectionPersister.java:728)
at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:291)
at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3303)
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:195)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71)
at net.sf.hibernate.collection.Bag.toString(Bag.java:482)
at java.lang.String.valueOf(String.java:2131)
at java.lang.StringBuffer.append(StringBuffer.java:370)
at com.godyo.dartscore.HibernateTest.getTeamPlayer(HibernateTest.java:130)
at com.godyo.dartscore.HibernateTest.main(HibernateTest.java:46)
Caused by: java.sql.SQLException: Syntax error or access violation message from server: "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '__, player0_.playerid as playerid__, player1_.UUID as UUID0_, p"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2251)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1586)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:88)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:875)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:990)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:965)
at net.sf.hibernate.loader.CollectionLoader.initialize(CollectionLoader.java:83)
at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:288)
... 8 more
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:201)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71)
at net.sf.hibernate.collection.Bag.toString(Bag.java:482)
at java.lang.String.valueOf(String.java:2131)
at java.lang.StringBuffer.append(StringBuffer.java:370)
at com.godyo.dartscore.HibernateTest.getTeamPlayer(HibernateTest.java:130)
at com.godyo.dartscore.HibernateTest.main(HibernateTest.java:46)
Caused by: net.sf.hibernate.exception.SQLGrammarException: could not initialize collection: [com.godyo.dartscore.hibernate.beans.Team.player#402880e50085a355010085a3594b0001]
at net.sf.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:69)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30)
at net.sf.hibernate.collection.AbstractCollectionPersister.convert(AbstractCollectionPersister.java:728)
at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:291)
at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3303)
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:195)
... 6 more
Caused by: java.sql.SQLException: Syntax error or access violation message from server: "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '__, player0_.playerid as playerid__, player1_.UUID as UUID0_, p"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2251)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1586)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:88)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:875)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:990)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:965)
at net.sf.hibernate.loader.CollectionLoader.initialize(CollectionLoader.java:83)
at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:288)
... 8 more
Name and version of the database you are using:
I use MySQL 4.0.13-nt
The generated SQL (show_sql=true):
look at the debug information
Debug level Hibernate log excerpt:
Hibernate 2.1.7
- hibernate.properties not found
- using CGLIB reflection optimizer
- using JDK 1.4 java.sql.Timestamp handling
- configuring from resource: /hibernate.cfg.xml
- Configuration resource: /hibernate.cfg.xml
- Mapping resource: Game.hbm.xml
- Mapping class: com.godyo.dartscore.hibernate.beans.Game -> GAME
- Mapping resource: Team.hbm.xml
- Mapping class: com.godyo.dartscore.hibernate.beans.Team -> TEAM
- Mapping collection: com.godyo.dartscore.hibernate.beans.Team.player -> teamplayerassignment
- Mapping resource: GameResult.hbm.xml
- Mapping class: com.godyo.dartscore.hibernate.beans.GameResult -> GAMERESULT
- Mapping resource: Player.hbm.xml
- Mapping class: com.godyo.dartscore.hibernate.beans.Player -> PLAYER
- Mapping collection: com.godyo.dartscore.hibernate.beans.Player.teams -> teamplayerassignment
- Configured SessionFactory: null
- processing one-to-many association mappings
- Mapping collection: com.godyo.dartscore.hibernate.beans.Game.GameResults -> GAMERESULT
- Mapping collection: com.godyo.dartscore.hibernate.beans.Team.games -> GAME
- processing one-to-one association property references
- processing foreign key constraints
- Using dialect: net.sf.hibernate.dialect.MySQLDialect
- Maximim outer join fetch depth: 2
- Use outer join fetching: true
- Using Hibernate built-in connection pool (not for production use!)
- Hibernate connection pool size: 20
- using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://azrael:3306/dartscore
- connection properties: {user=dartscore, password=dartscore}
- Transaction strategy: net.sf.hibernate.transaction.JDBCTransactionFactory
- No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
- Use scrollable result sets: true
- Use JDBC3 getGeneratedKeys(): true
- Optimize cache for minimal puts: false
- echoing all SQL to stdout
- Query language substitutions: {}
- cache provider: net.sf.hibernate.cache.EhCacheProvider
- query cache factory: net.sf.hibernate.cache.StandardQueryCacheFactory
- instantiating and configuring caches
- building session factory
- Not binding factory to JNDI, no JNDI name configured
- starting update timestamps cache at region: net.sf.hibernate.cache.UpdateTimestampsCache
- No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/webserver/instances/instance3/webapps/cocoon/WEB-INF/lib/ehcache-1.0.jar!/ehcache-failsafe.xml
- Could not find configuration for net.sf.hibernate.cache.UpdateTimestampsCache. Configuring using the defaultCache settings.
- starting query cache at region: net.sf.hibernate.cache.StandardQueryCache
- Could not find configuration for net.sf.hibernate.cache.StandardQueryCache. Configuring using the defaultCache settings.
Hibernate: select team0_.UUID as x0_0_ from TEAM team0_
Hibernate: select team0_.UUID as UUID0_, team0_.name as name0_, team0_.description as descript3_0_, team0_.last_modified as last_mod4_0_ from TEAM team0_ where team0_.UUID=?
Hibernate: select team0_.UUID as UUID0_, team0_.name as name0_, team0_.description as descript3_0_, team0_.last_modified as last_mod4_0_ from TEAM team0_ where team0_.UUID=?
ID of Team: 402880e50085a355010085a3594b0001
The table scripts:
CREATE TABLE `player` (
`UUID` varchar(32) NOT NULL default '',
`name` varchar(255) NOT NULL default '',
`description` varchar(255) NOT NULL default '',
`last_modified` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`UUID`)
) TYPE=MyISAM;
CREATE TABLE `team` (
`UUID` varchar(32) NOT NULL default '',
`name` varchar(255) NOT NULL default '',
`description` varchar(255) NOT NULL default '',
`last_modified` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`UUID`)
) TYPE=MyISAM;
CREATE TABLE `teamplayerassignment` (
`teamid` varchar(32) NOT NULL default '',
`playerid` varchar(32) NOT NULL default ''
) TYPE=MyISAM;