I've slow myself down a bit. I've try to map collection with unidirectional Associations. But it doesn't work either.
Code:
<class name="hibernate.Factory" table="factories">
<id name="id" type="integer" column="factoryid">
<generator class="native"/>
</id>
<property name="name" column="factoryname" type="string" not-null="true"/>
<set name="allProducts" lazy="true">
<key column="factoryid"/>
<one-to-many class="hibernate.Product"/>
</set>
</class>
Code:
<class name="hibernate.Product" table="products">
<id name="id" type="integer" column="productid">
<generator class="native"/>
</id>
<property name="name" column="productname" type="string" not-null="true" />
</class>
Code:
Product product = new Product();
product.setName("pa");
Factory factory = new Factory();
factory.setName("B");
Set allProducts = new HashSet();
allProducts.add(product);
factory.setAllProducts(allProducts);
Transaction tx = session.beginTransaction();
try {
session.save(factory);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
tx.rollback();
} finally {
session.close();
}
I generate following DDL wiht SchemaExport
Code:
drop table products
drop table factories
drop table people
create table products (
productid INTEGER NOT NULL AUTO_INCREMENT,
productname VARCHAR(255) not null,
factoryid INTEGER,
primary key (productid)
)
create table factories (
factoryid INTEGER NOT NULL AUTO_INCREMENT,
factoryname VARCHAR(255) not null,
primary key (factoryid)
)
create table people (
peopleid INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(255) not null,
birthdate DATE not null,
primary key (peopleid)
)
alter table products add index (factoryid), add constraint FKC42BD164B0E323A5 foreign key (factoryid) references factories (factoryid)
when excecute the exception occur
Code:
net.sf.hibernate.HibernateException: SQL update or deletion failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.collection.CollectionPersister.recreate(CollectionPersister.java:633)
at net.sf.hibernate.impl.ScheduledCollectionRecreate.execute(ScheduledCollectionRecreate.java:23)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2100)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2065)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2005)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:57)
at hibernate.UseHibernate.main(UseHibernate.java:53)
Please enlight me.
Best Regard