Monday, September 3, 2007

Hibernate: Annotation one-to-many/many-to-one

Bidirectonal one-to-many/many-to-one association

Hibernate Doc (Chap 8.4.1)

::Relationship::
person(many) <-> address(one)
- person A and person B live in address AA

::DB Schema::
person(personId, addressId)
address(addressId)


::Java Operation::
person.getAddress();
address.getPeople();

::Annotation::

@Entity
@Table(name="PERSON")
public class Person {
   
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name="personId")
  private int id;
   
  @ManyToOne
  @JoinColumn(name="addressId")     // inverse = false
  private Address address;
}


@Entity
@Table(name = "ADDRESS")
public class Address {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;

  @OneToMany(mappedBy="address")  // pointing Person's address field
  @Column(name="personId")    // inverse=true
  private Set<Person> people;
}



::Generated SQL::

[Association Mapping List]

10 comments:

dminer said...

@Column(name="personId") in Address table is little confusing to me. Does it mean that there will be a personId column in Address table ? What does it mean to have inverse=true ?

Since its Person(many) to Address(one), Person is the only table that will have addressId to keep the relation. If so, what does Column(name='personId') achieve in Address Table ?


-- Dminer

Narayana Enaganti said...

can u send a end to end example for me.. my id narayana49@gmail.com

alternateStory said...

Hi,

Thanks for your post! It's really helpful!

How ever, I am getting an error when I try to export schema -

Exception in thread "main" org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: ADDRESS, for columns: [org.hibernate.mapping.Column(personId)]

Any idea why?

Anonymous said...

The @Column is wrong in the Address class. It must be a @JoinColumn.

Anonymous said...

Nice post

Gopi said...

Hi,

In the above code, please help me out in making the foreign_key(addressid) as null while i delete the address entry from address table?

Thanks,
Gopi

Gopi said...

Hi,

If i delete the "Address" entity, i want to make the joinedcolumn in PERSON entity as "null"

How to achieve it, could you help this?

Thanks,
Gopi

mandar.bedekar said...

Plz some one tell me How i use one to many mapping in hibernate for creation of three tables.
1.discussion(discId,queId,ansId)
2.question(queId,question,postdate)
3.answer(ansId,answer,postDate)


Thanks in advance


Plz rply its argent

Sayan Guharoy said...

A typical metadata ORM model is create by xml mapping style,alternatively we can use hibernate annotation to design our persistance class.
See the below to example one with annotaion and one without it

http://fundapass.blogspot.in/2012/09/hibernate-with-annotation-example.html

http://fundapass.blogspot.in/2012/09/hibernate-one-to-many-mapping.html

Bajrang Kar said...

Not working for me.
In place of this, I tried to combine to Uni's & worked.
like
@OneToMany
@JoinColumn(name="sid")
private Set&rt;Address&rt; address;
and
@ManyToOne
@JoinColumn(name="sid")
private Student student;

Why it is not working with mappedBy?? Help!