Hi NG!
I want to use an embeddable object, that contains a collection (aka an association) to entity-objects.
This should work, because hibernate_annotations.pdf says:
"While not supported by the EJB3 specification, Hibernate Annotations allows you to use association annotations in an embeddable object (ie @*ToOne nor @*ToMany). To override the association columns you can use @AssociationOverride."
If I generate ddl with the hibernate-tool hbm2ddl, the ddl looks nice. (see bottom of posting). But the result of hibernate-tool hbm2hbmxml only shows:
<component name="personsDelegate" class="PersonsDelegate">
<!-- TODO -->
</component>
I would like to change to hbm2hbmxml-templates, but I do not know what to insert into the <component>-tags.
Any hint in the right direction?
Regards Berti
Appendix, the code:
I want to persist the entities Class A, Class B, Person and an Embedded Class PersonsDelegate. They look like this:
@Entity
public class A {
private PersonsDelegate personsDelegate;
public PersonsDelegate getPersonsDelegate() {
return personsDelegate;
}
public void setPersonsDelegate (PersonsDelegate personsDelegate) {
this.personsDelegate = personsDelegate;
}
// ...plus some other A-specifc properties
}
I've another class B that looks like class A, i.e. has also a property personsDelegate but it contains also other properties that differ from class A. The entity person ist just an bean holding the informations about a person.
PersonsDelegate looks like this:
@Embeddable
public class PersonsDelegate {
private List<Person> peoplesCollection = new ArrayList<Person>();
@OneToMany( fetch=FetchType.EAGER )
@Cascade(value={CascadeType.ALL, CascadeType.DELETE_ORPHAN})
public List<Person> getPeoplesCollection () {
return peoplesCollection ;
}
public void setPeoplesCollection (List<People> peoplesCollection ) {
this.peoplesCollection = peoplesCollection ;
}
public void addPerson(Person Person) {
peoplesCollection.add(Person);
}
public void removePerson(Person Person) {
peoplesCollection.remove(Person);
}
}
The generated DDL:
create table Person (
id varchar(255) not null,
name varchar(255),
primary key (id)
// ...some other stuff
);
create table A (
id varchar(255) not null,
primary key (id)
// ...some other stuff
);
create table B (
id varchar(255) not null,
primary key (id)
// ...some other stuff
);
create table A_Person (
A_id varchar(255) not null,
peoplesCollection_id varchar(255) not null,
unique (peoplesCollection_id)
);
create table B_Person (
B_id varchar(255) not null,
peoplesCollection_id varchar(255) not null,
unique (peoplesCollection_id)
);
alter table A_Person
add constraint FK2443D9E7C5183B47
foreign key (A_id)
references A;
alter table A_Person
add constraint FK2443D9E7A6B94834
foreign key (peoplesCollection_id)
references A;
alter table B_Person
add constraint FK2443D9E7C5183B47
foreign key (B_id)
references B;
alter table B_Person
add constraint FK2443D9E7A6B94834
foreign key (peoplesCollection_id)
references B;
The generated mapping-files look like this:
<!-- Generated 01.08.2007 11:21:48 by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
<class name="A" table="A" lazy="false">
<id name="id" type="java.lang.String">
<column name="id" />
<generator class="uuid"></generator>
</id>
<component name="personsDelegate" class="PersonsDelegate">
<!-- TODO -->
</component>
<!-- ...plus some other mappings -->
</class>
</hibernate-mapping>
|