Suggested Pages

Wednesday, June 27, 2012

JPA: ManyToOne Unidirectional Association

In this post we'll see an example of a unidirectional association ManyToOne.

Object point of view


Parent.java
...
@Entity
public class Parent {

 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 private int parentId;

 @Column
 private String name;

        ...
}

Child.java
  ...
@Entity
public class Child {
 
 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 private int childId;
 
 @Column
 private String name;

 @ManyToOne
 private Parent parent;
       
        ...
}

Database Point of View


create table Child (
        childId integer generated by default as identity (start with 1),
        name varchar(255),
        parent_parentId integer,
        primary key (childId)
)

create table Parent (
        parentId integer generated by default as identity (start with 1),
        name varchar(255),
        primary key (parentId)
)

alter table Child 
        add constraint FK3E104FC9E7EF479 
        foreign key (parent_parentId) 
        references Parent

No comments :

Post a Comment

Suggested Pages