-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Many to many association
PostPosted: Thu Feb 26, 2009 7:56 am 
Newbie

Joined: Thu Feb 26, 2009 7:14 am
Posts: 3
I have a problem with a many-to-many association with Strings, whose not the id of the classes.

My hbm-config;

<hibernate-mapping>
<class name="com.Benutzer" table="BENUTZER">

<id name="benutzerId" column="BENUTZER_ID" type="long">
<generator class="seqhilo">
<param name="sequence">SEQ_BENUTZER</param>
<param name="max_lo">10</param>
</generator>
</id>

<set name="rollen" table="BENUTZER_ROLLE" lazy="true" cascade="none" sort="unsorted">
<key column="NAME"></key>
<many-to-many class="com.Rolle" column="NAME1" outer-join="auto" />
</set>

<property name="name" type="java.lang.String" update="true" insert="true" column="NAME" not-null="true"/>
</class>

<class name="com.Rolle" table="ROLLE">

<id name="rolleId" column="ROLLE_ID" type="long">
<generator class="seqhilo">
<param name="sequence">SEQ_ROLLE</param>
<param name="max_lo">10</param>
</generator>
</id>

<set name="benutzer" table="BENUTZER_ROLLE" lazy="true"
cascade="none" sort="unsorted" >
<key column="NAME1" > </key>
<many-to-many class="com.Benutzer" column="NAME" outer-join="auto" />
</set>

<property name="name" type="java.lang.String" update="true" insert="true" column="NAME1"/>
</class>
</hibernate-mapping>


My db:

create table benutzer (benutzer_id number(38) not null, name VARCHAR2(50) not null CONSTRAINT PK_BENUTZER PRIMARY KEY (benutzer_id) )

create table rolle (rolle_id number(38) not null, name1 VARCHAR2(50) not null CONSTRAINT PK_ROLLE PRIMARY KEY (rolle_id) )

create table benutzer_rolle (name VARCHAR2(50) not null, name1 VARCHAR2(50) not null, CONSTRAINT PK_BENUTZER_rolle PRIMARY KEY (name, rolle_name) )


I got following error:
DEBUG: select rollen0_.NAME as NAME0_, rollen0_.NAME1 as NAME2_0_ from BENUTZER_ROLLE rollen0_ where rollen0_.NAME=?
2009-02-26 11:56:12,453 main, org.hibernate.util.JDBCExceptionReporter(77)
WARN : SQL Error: 1722, SQLState: 42000
Exception in thread "main" 2009-02-26 11:56:12,454 main, org.hibernate.util.JDBCExceptionReporter(78)
ERROR: ORA-01722: invalid number

org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.claas.ccs.ts.base.model.Benutzer.rollen#2673]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2001)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:63)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:163)
at com.claas.ccs.ts.base.model.Test.main(Test.java:23)
Caused by: java.sql.SQLException: ORA-01722: invalid number

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:745)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:966)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1062)
at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:850)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1134)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3339)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3384)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:75)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
at org.hibernate.loader.Loader.doQuery(Loader.java:674)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1994)
... 8 more


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2009 8:22 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
But why havnt you declared name and name1 of link table as the foreignkeys? Try that.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2009 8:28 am 
Newbie

Joined: Thu Feb 26, 2009 7:14 am
Posts: 3
Please give me an example


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2009 10:20 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
create table benutzer_rolle (name VARCHAR2(50) not null references benutzer(name), name1 VARCHAR2(50) not null references rolle(name1), CONSTRAINT PK_BENUTZER_rolle PRIMARY KEY (name, rolle_name) )

For this I think u ll have to make name n name1 unique

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2009 1:55 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
I was wrong. I tried this and found that in the DB you dont need a foreign key reference for the columns, but in the role and benutzer mappings the name1 and name properties need to be declared unique. Then inside the many-to-many mapping you can use property-ref to refer to the name and name1 properties instead of the primary keys.

Ie

<class name="com.Benutzer" table="BENUTZER">

.......
.......

<property name="name" type="java.lang.String" update="true" insert="true" column="NAME" not-null="true" unique="true"/>

<set name="rollen" table="BENUTZER_ROLLE" lazy="true" cascade="none" sort="unsorted">
<key column="NAME"></key>
<many-to-many class="com.Rolle" property-ref="name1" column="NAME1" outer-join="auto" />
</set>
</class>

<class name="com.Rolle" table="ROLLE">

.......
.......

<property name="name" type="java.lang.String" update="true" insert="true" column="NAME1" unique="true"/>

<set name="benutzer" table="BENUTZER_ROLLE" lazy="true"
cascade="none" sort="unsorted" >
<key column="NAME1"> </key>
<many-to-many class="com.Benutzer" property-ref="name" column="NAME" outer-join="auto" />
</set>
</class>

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2009 2:33 am 
Newbie

Joined: Thu Feb 26, 2009 7:14 am
Posts: 3
Great! Thanks for your help!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2009 3:01 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Rating is welcome..... :-)

_________________
Regards,
Litty Preeth


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.