-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to generate a Collection of objects?
PostPosted: Mon May 22, 2006 1:42 pm 
Newbie

Joined: Wed May 17, 2006 11:52 am
Posts: 5
I try to get the following: One user can have many Trainings. How can I achieve that?
I think I need one-to-many or many-to-one. Whats the difference between the both?

Here the SQL statements for creating the tables, so you know my structure:

Code:
create table saTaUser( 
    sUserId varchar(10) not null, 
    sVorname varchar(30) not null, 
    sNachname varchar(30) not null, 
    sPLZ varchar(10) not null, 
    sOrt varchar(30) not null, 
    sStrasse varchar(30) not null, 
    primary key (sUserId) 
)

create table saTaTraining (
    nId identity,
    tEinheitStart date not null,
    tEinheitEnde date not null,
    sBeschreibung varchar(200) not null,
    nAnzahlKm double not null,
    sUserId varchar(10),
    primary key (nId),
    foreign key (sUserId) references saTaUser(sUserId) on delete cascade
)


Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="sa.bo.UserDAO" table="saTaUser">
      <id name="Id" >
            <column name="sUserId" not-null="true"/>
        </id>
       
        <property name="Vorname">
            <column name="sVorname" length="60" not-null="false" />
        </property>
       
        <property name="Nachname">
           <column name="sNachname" length="60" not-null="false" />
        </property>
       
        <property name="Plz">
           <column name="sPlz" length="30" not-null="false" />
        </property>
       
        <property name="Ort">
           <column name="sOrt" length="60" not-null="false" />
        </property>
       
        <property name="Strasse">
           <column name="sStrasse" length="60" not-null="false" />
        </property>
       
       <set name="Trainings">
           <key column="sUserId" not-null="true"/>
           <one-to-many class="sa.bo.TrainingDAO"/>
       </set>
    </class>
   
    <class name="sa.bo.TrainingDAO" table="saTaTraining">
      <id name="Id">
           <column name="nId" not-null="true" />
           <generator class="identity">
           
           </generator>
        </id>   
       
        <property name="EinheitStart">
            <column name="tEinheitStart" not-null="false" />
        </property>
       
        <property name="EinheitEnde">
           <column name="tEinheitEnde" not-null="false" />
        </property>
       
        <property name="Beschreibung">
           <column name="sBeschreibung" length="240" not-null="false" />
        </property>
       
        <property name="AnzahlKm">
           <column name="nAnzahlKm" not-null="false" />
        </property>     
       
        <property name="UserId">
           <column name="sUserId" not-null="false" />
        </property>
    </class>
</hibernate-mapping>



Full stack trace of any exception that occurs:
Code:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.ExceptionInInitializerError
   at util.HibernateUtil.<clinit>(HibernateUtil.java:39)
   at sa.admin.Userverwaltung.find(Userverwaltung.java:242)
   at sa.admin.Userverwaltung.main(Userverwaltung.java:78)
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: sa.bo.TrainingDAO column: sUserId (should be mapped with insert="false" update="false")
   at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:575)
   at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:597)
   at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:615)
   at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:405)
   at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
   at org.hibernate.cfg.Configuration.validate(Configuration.java:984)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1169)
   at util.HibernateUtil.<clinit>(HibernateUtil.java:34)
   ... 2 more



Name and version of the database you are using:
HSQL


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 10:13 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Many-to-one is used for the reverse direction. User has a one-to-many relation with Training, Training has a many-to-one relation with User.

Have you removed something from your mapping? The error says that sUserId is mapped twice in Training, but it's not mapped twice in the mapping you've posted.

I'm guessing from your mapping that you want to use the sUserId column in saTaTraining to join with the sUserId column in saTaUser. Your mapping is joining on the nId column. To fix that, you either have to add in the many-to-one association from Training to User, or change your mapping to a many-to-many so that you can add a property-ref attribute.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 11:16 pm 
Beginner
Beginner

Joined: Sat Jan 31, 2004 7:19 pm
Posts: 39
You should take the hint in the exception literally and everything
should fall into place. At the end of sa.bo.TrainingDAO:

Code:
        ...
        <property name="UserId" insert="false" update="false">
           <column name="sUserId" not-null="false" />
        </property>


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 11:25 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That sidesteps the problem, but it doesn't explain it. Plus, when that mapping gets converted to many-to-one (and it almost certainly should be a many-to-one, IDs shouldn't be mapped using <property> without good reason), having it insert=false update=false means that you won't ever be able to put any Training into a User's collection.

Better to investigate now than simply make the error disappear.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 3:56 am 
Newbie

Joined: Wed May 17, 2006 11:52 am
Posts: 5
Quote:
Have you removed something from your mapping?

No, I didn't. The error occured with the mapping I posted.

I don't get it working. Can you post the mapping file that is needed for my example?

What I exactly want: The User should have a collection which contains all Trainings where the sUserId is the UserId.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 7:02 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Code:
<class name="User" ...>
  ...
  <set name="Trainings" ...>
    <key column="sUserId"/>
    <one-to-many class="Training"/>
  </set>
  ...
</class>

<class name="Training" ...>
  ...
  <many-to-one name="User" column="sUserId" .../>
  ...
</class>
You shouldn't map sUserId in Training as a property. If you need it as a property, you can map it, but you must use insert="false" update="false".

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 4:17 am 
Newbie

Joined: Wed May 17, 2006 11:52 am
Posts: 5
Hi,

I reached a point where no errors occur. But I have the problem that there are no elements in the set.



My mapping file:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="sa.bo.UserDAO" table="saTaUser">
      <id name="Id" >
            <column name="sUserId" not-null="true"/>
        </id>
       
        <property name="Vorname">
            <column name="sVorname" length="60" not-null="false" />
        </property>
       
        <property name="Nachname">
           <column name="sNachname" length="60" not-null="false" />
        </property>
       
        <property name="Plz">
           <column name="sPlz" length="30" not-null="false" />
        </property>
       
        <property name="Ort">
           <column name="sOrt" length="60" not-null="false" />
        </property>
       
        <property name="Strasse">
           <column name="sStrasse" length="60" not-null="false" />
        </property>
       
       <set name="TrainingSet" table="saTaUser">
           <key column="sUserId" />
           <one-to-many class="sa.bo.TrainingDAO"/>
       </set>
    </class>
   
    <class name="sa.bo.TrainingDAO" table="saTaTraining">
      <id name="Id">
           <column name="nId" not-null="true" />
           <generator class="identity">
           
           </generator>
        </id>   
               
        <property name="EinheitStart">
            <column name="tEinheitStart" not-null="false" />
        </property>
       
        <property name="EinheitEnde">
           <column name="tEinheitEnde" not-null="false" />
        </property>
       
        <property name="Beschreibung">
           <column name="sBeschreibung" length="240" not-null="false" />
        </property>
       
        <property name="AnzahlKm">
           <column name="nAnzahlKm" not-null="false" />
        </property>     
       
        <property name="UserId">
           <column name="sUserId" not-null="false" />
        </property>
       
        <!-- <many-to-one name="User" class="sa.bo.UserDAO" column="sUserId" /> -->
    </class>
</hibernate-mapping>




That's my set (defined in UserDAO):
Code:
private Set<TrainingDAO> _soTrainingSet = new HashSet<TrainingDAO>();



The getter and setter methods of the Set:
Code:
    public Set<TrainingDAO> getTrainingSet()
    {
        return _soTrainingSet;
    }

    public void setTrainingSet(Set<TrainingDAO> soTrainings)
    {
        _soTrainingSet = soTrainings;
    }


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 28, 2006 6:05 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I'm guessing that it has something to do with your join. In UserDAO, you're using sUserId as the key to the set: that looks like a string of some sort, though you haven't specified the type (you really should be explicit about types: hibernate might be able to figure it out automatically, but we mere mortals can't...). In TrainingDAO, the PK is nId, which I'm guessing is an int of some kind. If that's the case, it's probable that no rows are matching the join, hence your set is correctly empty.

_________________
Code tags are your friend. Know them and use them.


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