Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
3
Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.ternasoft.crm.model.Kunde" table="kunde" catalog="hds_test">
<id name="kundenid" type="java.lang.Integer">
<column name="kundenid" />
<generator class="increment" />
</id>
<many-to-one name="firma" class="com.ternasoft.crm.model.Firma" fetch="select">
<column name="firmaid" />
</many-to-one>
<many-to-one name="agentByAgent" class="com.ternasoft.crm.model.Agent" fetch="select">
<column name="agent" />
</many-to-one>
<many-to-one name="filiale" class="com.ternasoft.crm.model.Filiale" fetch="select">
<column name="filiale" />
</many-to-one>
<many-to-one name="agentByTerminvereinbarer" class="com.ternasoft.crm.model.Agent" fetch="select">
<column name="terminvereinbarer" />
</many-to-one>
<many-to-one name="kundenstatus" class="com.ternasoft.crm.model.Kundenstatus" fetch="select">
<column name="kstatus" />
</many-to-one>
<many-to-one name="karte" class="com.ternasoft.crm.model.Karte" fetch="select">
<column name="kartenid" />
</many-to-one>
<property name="telefon" type="java.lang.String">
<column name="telefon" length="20" />
</property>
<property name="email" type="java.lang.String">
<column name="email" length="50" not-null="false" />
</property>
<property name="aktivierungsdatum" type="java.util.Date">
<column name="aktivierungsdatum" length="19" />
</property>
<property name="letztlogin" type="java.util.Date">
<column name="letztlogin" length="19" />
</property>
<property name="aktivierungscode" type="java.lang.String">
<column name="aktivierungscode" length="10" />
</property>
<property name="fremdkundenid" type="java.lang.Integer">
<column name="fremdkundenid" />
</property>
<property name="importStamp" type="java.lang.String">
<column name="importStamp" length="17" />
</property>
<property name="beginn" type="java.util.Date">
<column name="beginn" length="10" />
</property>
<property name="ende" type="java.util.Date">
<column name="ende" length="10" />
</property>
<property name="erfassungsdatum" type="java.util.Date" formula="current_timestamp()" >
<column name="erfassungsdatum" length="19" />
</property>
<set name="persons" inverse="true" cascade="all">
<key>
<column name="kundenid" />
</key>
<one-to-many class="com.ternasoft.crm.model.Person" />
</set>
<set name="notizs" inverse="true">
<key>
<column name="kundenid" />
</key>
<one-to-many class="com.ternasoft.crm.model.Notiz" />
</set>
<set name="zahlungsdatens" inverse="true">
<key>
<column name="kundenid" />
</key>
<one-to-many class="com.ternasoft.crm.model.Zahlungsdaten" />
</set>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.ternasoft.crm.model.Person" table="person" catalog="hds_test">
<id name="personid" type="java.lang.Integer">
<column name="personid" />
<generator class="increment" />
</id>
<many-to-one name="land" class="com.ternasoft.crm.model.Land" fetch="select">
<column name="land" />
</many-to-one>
<many-to-one name="kunde" class="com.ternasoft.crm.model.Kunde" fetch="select" cascade="all">
<column name="kundenid" />
</many-to-one>
<property name="titel" type="java.lang.String">
<column name="titel" length="10" />
</property>
<property name="anrede" type="java.lang.String">
<column name="anrede" length="10" />
</property>
<property name="vorname" type="java.lang.String">
<column name="vorname" length="20" not-null="true" />
</property>
<property name="nachname" type="java.lang.String">
<column name="nachname" length="20" />
</property>
<property name="strasse" type="java.lang.String">
<column name="strasse" length="50" />
</property>
<property name="plz" type="java.lang.String">
<column name="plz" length="10" not-null="true" />
</property>
<property name="ort" type="java.lang.String">
<column name="ort" length="50" not-null="true" />
</property>
<property name="istkunde" type="java.lang.Byte">
<column name="istkunde" not-null="true" />
</property>
<property name="telefon" type="java.lang.String">
<column name="telefon" length="50" />
</property>
<property name="email" type="java.lang.String">
<column name="email" length="100" />
</property>
<property name="geburtsdatum" type="java.util.Date">
<column name="geburtsdatum" length="10" />
</property>
<set name="bestellungs" inverse="true">
<key>
<column name="personid" not-null="true" />
</key>
<one-to-many class="com.ternasoft.crm.model.Bestellung" />
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Transaction tx=null;
try {
KundeDAO kdao = new KundeDAO();
tx = kdao.getSession().beginTransaction();
Kunde nk = new Kunde();
nk.setAktivierungscode("asdfas");
nk.setAktivierungsdatum(new Date());
nk.setBeginn(new Date());
nk.setEnde(new Date());
nk.setFremdkundenid(2342342);
nk.setImportStamp("3234234");
Set persons = new HashSet();
Person np = new Person();
np.setVorname("Max");
np.setNachname("Muster");
np.setAnrede("Herr");
np.setEmail("csad242@asdf.com");
np.setPlz("6890");
np.setStrasse("asdfsa");
np.setGeburtsdatum(new Date());
np.setIstkunde(new Byte((byte) 1));
np.setOrt("Lustenau");
persons.add(np);
nk.setPersons(persons);
kdao.save(nk);
tx.commit();
} catch (CrmException e) {
tx.rollback();
// TODO Auto-generated catch block
e.printStackTrace();
}
Full stack trace of any exception that occurs:Name and version of the database you are using:MySql
The generated SQL (show_sql=true):Code:
Hibernate: select max(kundenid) from kunde
Hibernate: select max(personid) from person
Hibernate: insert into hds_test.kunde (firmaid, agent, filiale, terminvereinbarer, kstatus, kartenid, telefon, email, aktivierungsdatum, letztlogin, aktivierungscode, fremdkundenid, importStamp, beginn, ende, kundenid) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into hds_test.person (land, kundenid, titel, anrede, vorname, nachname, strasse, plz, ort, istkunde, telefon, email, geburtsdatum, personid) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Debug level Hibernate log excerpt:Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.htmlHelloo!!
I am newbie in hibernate, before I use JDBC methods to connect database. I have some questions which dont understand:
1) I have a MySql database and all the primary keys of the tables use "autoincrement" which generator class must I use?
2) I have some trouble with inserting (saving) a object with a associate collection. In the mapping files I write cascading="all", I use autoincrement generator. But when I save a object with an associated set the referenced table has as foreign key null?
3) How can I map a one-to-one when i have for exsample
Table:customer
customerid (Primary key)
name (String)
Table:adress
adress (String)
customerid (Foreign key)
mainadress (Boolean)
how I create my customer.hbm.xml
when I want a pojo like this
public customer {
....
Set adresses; //all adresses or better all with mainadress=0
Adress mainadress; //adress with mainadress=1
....
}
I know for one-to-one I need a key from adress in customer but the db is in use.
Thank you lot for your answers!
Code:
Code:
[quote][/quote]