This article demonstrates, rather shortcut, how we can create persistence objects:
//Base Class
@MappedSuperclass
public abstract class BaseClass implements Interface{
@Id
@Column(name = “id”)
@GeneratedValue
@Override
private int id
@Transient
private String unwantedField;
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
}
//Child Class
@Entity
@Table(name = “table_name”)
public class ChildClass extends BaseClass{
@Column(name = “user_name”)
private String useName;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@JoinColumn(name = “id”)
@Override
private List
}
//Associated class
@Entity
@Table(name = “another_table”)
public class AnotherClass {
@Id
@Column(name = “comment_id”) //this can not be “id”, because “id” is referenced from ChildClass
@GeneratedValue
private int id;
@ManyToOne
@JoinColumn(name = “id”) //this id is child class identifier column
private ChildClass child;
}
In above example, we implemented one to many relationship from ChildClass to AnotherClass instances.(Getters and setters not shown above). So, the “id” in “another_table” is foreign key from the table “table_name”.
[Note: Annotations should be either at properties definition or at getters(CAN’T be mixed).]