-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: @CollectionOfElements and jointable
PostPosted: Thu Jun 03, 2010 7:38 am 
Newbie

Joined: Tue May 25, 2010 5:51 pm
Posts: 8
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.


Top
 Profile  
 
 Post subject: Re: @CollectionOfElements and jointable
PostPosted: Thu Jun 03, 2010 2:10 pm 
Newbie

Joined: Tue May 25, 2010 5:51 pm
Posts: 8
a little help?


Top
 Profile  
 
 Post subject: Re: @CollectionOfElements and jointable
PostPosted: Thu Jun 03, 2010 4:21 pm 
Newbie

Joined: Tue May 25, 2010 5:51 pm
Posts: 8
Finally i think to find a solution... i do this:
Code:
@Entity
public class Municipality implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String country;
private String province;
private String name;
@OneToMany(mappedBy="address.municipality")
private List<Person> persons;

public Municipality() {
}

...

I have not changed other classes.

Is this correct or work only by chance?

Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.