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]
7 comments:
@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
can u send a end to end example for me.. my id narayana49@gmail.com
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?
The @Column is wrong in the Address class. It must be a @JoinColumn.
Nice post
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
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
Post a Comment