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