blow, if you do not really want *MUNICIPALITY_ADDRESSLIST* Table, you should use @Transient instead of @CollectionOfElements
public class Municipality {
@Transient
private List<Address> addressList;
}
If you use @CollectionOfElements, Hibernate will always create *MUNICIPALITY_ADDRESSLIST* Table. If @Transient does not fullfil your needs, provide more info about what you want to get
UPDATE
According to info provided by yourself
Person has an @Embedded Address which has a @ManyToOne relationship with Municipality
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Embedded
private Address address;
}
Now, our Address
/*
* @Embeddable class MUST implements Serializable
*/
@Embeddable
public class Address implements Serializable {
@ManyToOne
/*
* If you are using Hibernate INSTEAD OF JPA
* Prefer to use @Cascade Hibernate annotation, as shown bellow,
* instead of JPA cascade Attribute
*
* Also notice it can not be null - see nullable Attribute
*/
@Cascade(CascadeType.SAVE_UPDATE)
@JoinColumn(name="MUNICIPALITY_ID", nullable=false)
private Municipality municipality;
}
And Municipality (without ANY List of Address or Person)
public class Municipality {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
}
As shown above, your DB structure should looks like
PERSON
ID
MUNICIPALITY_ID
MUNICIPALITY
ID
But whether Municipality has a List of Person as follows
public class Municipality {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@OneToMany
private List<Person> personList;
}
You will get The same structure shown above
But you have a bi-directional relationship Person can see Municipality (Through Address class) and Municipality can see Person (Through List of Person)
Now let's see The following scenario
Person person = new Person();
Municipality municipality = new Municipality();
Address address = new Address();
address.setMunicipality(person);
person.setAddress(address);
session.save(person);
You will see
INSERT INTO PERSON blah, blah, blah...
Because of @Cascade(SAVE_UPDATE)
INSERT INTO MUNICIPALITY blah, blah, blah...
But because of The bi-directional relationship, You should set up which property is The owner of The relationship. Owner of The relationship ? What is that ?
Suppose
public class A {
@ManyToOne
private B b;
}
And
public class B {
@OneToMany
@JoinColumn(name="B_ID")
@Cascade(SAVE_UPDATE)
private List<A> aList;
}
And you do
/*
* a.getB() is null, right ?
A a = new A();
B b = new B();
b.getAList().add(a);
SQL outputs
/* Let's suppose B_ID generated value is 5
INSERT INTO B
/*
* because of @Cascade(SAVE_UPDATE)
INSERT INTO A (B_ID) VALUES(5)
But because a.getB() is null
You will also see
UPDATE A SET B_ID = null
It explains why you should use mappedBy attribute when using a bi-directional relationship.