Hi all.
I have this:
Municipality class
Code:
@Entity
public class Municipality implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String country;
private String province;
private String name;
@Column(name="cod_catasto")
private String codCatastale;
private String cap;
@CollectionOfElements
private List<Address> addressList;
public Municipality() {
}
...
Address class
Code:
@Embeddable
public class Address implements Serializable {
@ManyToOne(fetch=FetchType.LAZY)
@Cascade(CascadeType.SAVE_UPDATE)
private Municipality municipality;
@Column(length=45)
private String street;
public Address() {
}
...
Address is embedded in another class, Person.
Code:
@Entity
public class Person implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
private String surname;
@Embedded
private Address address;
public Person() {
}
When i save an instance of Person, hibernate create 3 tables: PERSON, MUNICIPALITY and MUNICIPALITY_ADDRESSLIST.
MUNICIPALITY_ADDRESSLIST contains 2 fields: MUNICIPALITY_ID (FK) and STREET.
I think this table is totally useless, in fact it is void(no records always) and i don't want this table, i only want the ID of table MUNICIPALITY into table PERSON(that embeds Address), what should i do?
I tried to add @JoinTable in Municipality entity like this:
Code:
@CollectionOfElements
@JoinTable(name="person")
private List<Address> addressList;
It partially worked,
but i cant choose the column name of table PERSON that contains ID of the table MUNICIPALITY, it is, by hibernate choose, simply "MUNICIPALITY_ID"(i want to change this, for example set to ID_MUNICIPALITY)...
I tried this too
Code:
@CollectionOfElements
@JoinTable(name="person", joinColumns=@JoinColumn(name="id_municipality"))
private List<Address> addressList;
When i save the FIRST instance of Person the PERSON table is this, like the example above:
Code:
ID STREET NAME SURNAME MUNICIPALITY_ID
When i save the SECOND instance of Person the PERSON table is now this:
Code:
ID STREET NAME SURNAME MUNICIPALITY_ID ID_MUNICIPALITY
In both case, the ID of table MUNICIPALITY is stored in column MUNICIPALITY_ID(choose automatically by hibernate...), the field ID_MUNICIPALITY is always NULL :(
What i want is simply to store ID in column ID_MUNICIPALITY (it is a name choose by me and NOT by hibernate).
Thanks.