Hi NG!
I'm learning hibernate and have a - IMO - common task to solve.
I've 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);
}
}
So far, so good, according to my hibernate-book this should work.
I use the latest hibernate-tools, and generate a DDL, which - IMO - looks quite good:
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;
But 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>
The interesting point - the component personsDelegate - is not implemented in the tools, which is clearly expressed by the generated TODO-comment.
But apart from the hibernate-tools, I don't have the slightest idea what I have to write manually in the component-tag to make the whole thing work? At the moment, no person will be persisted if I add one to class A or class B.
Everey hint is really appreciated.
Regards,
Berti
|