-->
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.  [ 4 posts ] 
Author Message
 Post subject: many-to-many and additional field in the connecting table
PostPosted: Tue Oct 16, 2007 1:10 pm 
Newbie

Joined: Tue Sep 18, 2007 3:30 pm
Posts: 17
Hi,

I have something like this:

<class name="org.dyndns.orzekanie.Application" table="APPLICATIONS" lazy="false">
<id name="id" type="integer" column="ID" >
<generator class="native"/>
</id>
...
<bag name="illnesses" table="APPLICATIONS_ILLNESSES" lazy="false">
<key column="application_id"></key>
<many-to-many column="illness_id" class="org.dyndns.orzekanie.application.Illness"/>
</bag>
...
</class>

<class name="org.dyndns.orzekanie.application.Illness" table="illnesses" lazy="false">
<id name="id" column="id" type="integer">
<generator class="assigned"/>
</id>
<property name="name"/>
</class>


I have in my Application object a List of Illness objects:

private List<Illness> illnesses;

I have also a table APPLICATIONS_ILLNESSES which connects two tables: APPLICATIONS and ILLNESSES. The problem is that I need a field which describes a particular illness in a particular application. I think the new field "description" should be added in the table APPLICATIONS_ILLNESSES:

APPLICATIONS_ILLNESSES {
application_id INTEGER;
illness_id INTEGER;
description VARCHAR(50);
}

but how tell hibernate to use this field? I think that my Illness class should have a new field:

public class Illness extends WithIdVo {
private Integer id;
private String name;

//the new field from APPLICATIONS_ILLNESSES table
private String description;
}

but I have no idea how it should be set in the hibernate xml file...

Could anyone suggest me how should I implement this?

Thanks,
Pawel



Hibernate version: 3.2.24

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using: MySQL 4.1

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 4:23 pm 
Beginner
Beginner

Joined: Fri Jun 25, 2004 11:47 am
Posts: 34
Hi,

The property "description" need to be set on the ApplicationsIllnesses java object and specified in the corresponding mapping file.
You would then access it the following way
Code:
Application.iterator().next().getDescription()


regards.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 5:14 pm 
Newbie

Joined: Tue Sep 18, 2007 3:30 pm
Posts: 17
I have no such object ApplicationIllnesses. I thought it was not necessary to have such one but now I've created it.

The application mapping file looks like this:

<class name="org.dyndns.orzekanie.Application" table="APPLICATIONS" lazy="false">
<id name="id" type="integer" column="ID" >
<generator class="native"/>
</id>
...
<bag name="illnesses" table="APPLICATIONS_ILLNESSES" lazy="false">
<key column="application_id"></key>
<many-to-many column="illness_id" class="org.dyndns.orzekanie.application.Illness"/>
</bag>
...
<bag name="illnessesDesc" table="APPLICATIONS_ILLNESSES" lazy="false">
<key column="application_id"></key>
<many-to-many class="org.dyndns.orzekanie.application.ApplicationIllness" />
</bag>
...
</class>


the ApplicationIllness mapping file:

<class name="org.dyndns.orzekanie.application.ApplicationIllness" table="APPLICATIONS_ILLNESSES" lazy="false">
<id name="applicationId" column="application_id">
<generator class="assigned"/>
</id>
<property name="illnessId" column="illness_id"/>
<property name="description" column="description"/>
</class>

and the ApplicationIllness class:

public class ApplicationIllness {
private int applicationId;
private int illnessId;
private String description;

//getters and setters
}



Now I have an error (the hibernate query is bad):


Hibernate:
/* load collection org.dyndns.orzekanie.Application.illnessesDesc */ select
illnessesd0_.application_id as applicat1_1_,
illnessesd0_.elt as elt1_,
applicatio1_.application_id as applicat1_10_0_,
applicatio1_.illness_id as illness2_10_0_,
applicatio1_.description as descript3_10_0_
from
APPLICATIONS_ILLNESSES illnessesd0_
left outer join
APPLICATIONS_ILLNESSES applicatio1_
on illnessesd0_.elt=applicatio1_.application_id
where
illnessesd0_.application_id=?

org.hibernate.exception.SQLGrammarException: could not initialize a collection: [org.dyndns.orzekanie.Application.illnessesDesc#97756]
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:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:454)
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:794)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:241)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3044)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:395)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:179)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:103)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
at org.dyndns.orzekanie.service.ApplicationService.getApplication(ApplicationService.java:33)
at org.apache.jsp.jsp.forms.application_jsp._jspService(application_jsp.java:135)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.dyndns.orzekanie.filter.AuthenticationFilter.doFilter(AuthenticationFilter.java:30)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: Unknown column 'illnessesd0_.elt' in 'field list'
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2001)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1168)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1279)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2281)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1634)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:92)
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)
... 44 more



What am I doing wrong?

Pawel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 17, 2007 8:09 am 
Newbie

Joined: Tue Sep 18, 2007 3:30 pm
Posts: 17
I have done that:

Main class Application has a collection of ApplicationIllness objects. Mapping file:

<class name="org.dyndns.orzekanie.Application" table="APPLICATIONS" lazy="false">
<id name="id" type="integer" column="ID" >
<generator class="native"/>
</id>
...
<bag name="illnessesDesc" table="APPLICATIONS_ILLNESSES" lazy="false">
<key column="application_id" not-null="true"/>
<one-to-many class="org.dyndns.orzekanie.application.ApplicationIllness"/>
</bag>
</class>


mapping file for ApplicationIllness:

<class name="org.dyndns.orzekanie.application.ApplicationIllness" table="APPLICATIONS_ILLNESSES" lazy="false">
<id name="illnessId" column="illness_id">
<generator class="assigned"/>
</id>
<property name="description"/>
</class>


The class ApplicationIllness looks like this:

public class ApplicationIllness {
private int applicationId;
private int illnessId;
private String description;

//getters and setters
}


Now getting object from hibernate is working properly. But updating it generates the output:

/* create one-to-many row org.dyndns.orzekanie.Application.illnessesDesc */ update
APPLICATIONS_ILLNESSES
set
application_id=?
where
illness_id=?
2007-10-17 14:01:03,125 DEBUG [http-8080-Processor23] hibernate.type.IntegerType - binding '70999' to parameter: 1
2007-10-17 14:01:03,125 DEBUG [http-8080-Processor23] hibernate.type.IntegerType - binding '1' to parameter: 2


which ends with:

2007-10-17 14:01:03,343 WARN [http-8080-Processor23] hibernate.util.JDBCExceptionReporter - SQL Error: 1062, SQLState: 23000
2007-10-17 14:01:03,343 ERROR [http-8080-Processor23] hibernate.util.JDBCExceptionReporter - Duplicate key or integrity constraint violation
message from server: "Duplicate entry '70999-10' for key 1"


My main key in the table APPLICATIONS_ILLNESSES consists of columns application_id and illness_id. The table looks like this:

APPLICATIONS_ILLNESSES {
application_id INTEGER;
illness_id INTEGER;
description VARCHAR(50);
}


I would appreciate any help,

Pawel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.