Hibernate's attention to attention is indeed very convenient for us to deal with the relationship between entities and sets, and can process sets through cascade methods, but sometimes attention to attention cannot meet our needs, for example, if you select a course, a typical multi-to-many relationship is generated.
Course_user (course_id, user_id );
However, it is best to add the review function when selecting a course, so we want to add a Boolean field to the automatically generated table in the middle, similar to this structure:
Course_user (course_id, user_id, accessable );
In this case, we can split the tables to tables into two tables to one, and manually add an intermediate table, which makes expansion much easier.
The Code is as follows:
@ Embeddable
Public class courseuserpk implements serializable {
Private Static final long serialversionuid = 1l;
Private Course course;
Private user;
@ Manytoone
@ Joincolumn (name = "course_id", nullable = false)
Public Course getcourse (){
Return course;
}
Public void setcourse (Course course ){
This. Course = course;
}
@ Manytoone
@ Joincolumn (name = "user_id", nullable = false)
Public user getuser (){
Return user;
}
Public void setuser (User user ){
This. User = user;
}
Public Boolean equals (Object object ){
If (this = Object)
Return true;
If (! (Object instanceof courseuserpk ))
Return false;
Final courseuserpk Other = (courseuserpk) object;
If (this. Course! = NULL & other. getcourse ()! = NULL ){
If (course. GETID () = Other. Course. GETID ()){
If (user! = NULL & other. getuser ()! = NULL & User. GETID () = Other. getuser (). GETID ()){
Return true;
} Else {
Return false;
}
} Else {
Return true;
}
} Else
Return false;
}
Public int hashcode (){
Return super. hashcode () +
(Course! = NULL? Course. hashcode (): 0) +
(User! = NULL? User. hashcode (): 0 );
}
}
@ Entity
@ Idclass (courseuserpk. Class)
Public class courseteacher {
Private Course course;
Private user;
Private Boolean accessable; // true user is in course, otherwise not
Public courseteacher (Course course, user, Boolean accessable ){
This. Course = course;
This. User = user;
This. accessable = accessable;
}
@ ID
Public Course getcourse (){
Return course;
}
Public void setcourse (Course course ){
This. Course = course;
}
@ ID
Public user getuser (){
Return user;
}
Public void setuser (User user ){
This. User = user;
}
Public Boolean getaccessable (){
Return accessable;
}
Public void setaccessable (Boolean accessable ){
This. accessable = accessable;
}
}
@ Entity
@ Idclass (courseuserpk. Class)
Public class coursestudent {
Private Course course;
Private user;
Private Boolean accessable; // true user is in course, otherwise not
Public coursestudent (Course course, user, Boolean accessable ){
This. Course = course;
This. User = user;
This. accessable = accessable;
}
@ ID
Public Course getcourse (){
Return course;
}
Public void setcourse (Course course ){
This. Course = course;
}
@ ID
Public user getuser (){
Return user;
}
Public void setuser (User user ){
This. User = user;
}
Public Boolean getaccessable (){
Return accessable;
}
Public void setaccessable (Boolean accessable ){
This. accessable = accessable;
}
}
@ Entity
Public class course extends baseentity {
Private Static final long serialversionuid = 8768227695335084711l;
Private set <courseteacher> teachers;
Private set <coursestudent> students;
@ Onetoetype (mappedby = "course", cascade = cascadetype. All)
Public set <courseteacher> getteachers (){
Return teachers;
}
Public void setteachers (set <courseteacher> teachers ){
This. Teachers = teachers;
}
@ Onetovel (mappedby = "Course ")
Public set <coursestudent> getstudents (){
Return students;
}
Public void setstudents (set <coursestudent> students ){
This. Students = students;
}
}
Reference: http://www.4ucode.com/Study/Topic/1070774
Hibernate can use the *. HBM. xml configuration file to split many-to-many relationships into two one-to-many relationships, but this point is not mentioned in the hibernate annotation document. The following example illustrates how to use annotations to split multiple-to-multiple data into two one-to-multiple data types.
The following describes the many-to-many relationship between product and order.
Product attributes include:
The attributes of order include:
Why do we need to split the many-to-many relationship into two one-to-many relationships?
Because many-to-many relationships cannot save attributes that are common to two objects. For example, how to record the quantity of product B purchased in order? If you use multiple-to-multiple ing, it cannot be implemented.
Intermediate entity-used to record the relationships between two many-to-many entities
In the relationship between product and order, an orderitem entity can be used to represent one of the many relations between the two. That is, the relationship between product, orderitem, order, and orderitem is one-to-many.
Attributes of orderitem include:
- Product (if product C is recorded currently)
- Order (if order D is recorded currently)
- Quantity (the number of item C in order D is recorded here)
Instance code (class package reference omitted): Composite primary key class:
@Embeddablepublic class OrderItemPK implements Serializable{ private Product product; private Order order; @ManyToOne @JoinColumn(name="product_id",referencedColumnName="id") public Product getProduct(){ return product; } public void setProduct(Product product){ this.product=product; } @ManyToOne @JoinColumn(name="order_id",referencedColumnName="id") public Order getOrder(){ return order; } public void setOrder(Order order){ this.order=order; } public boolean equals(Object object){...} public int hashCode(){...}}
Orderitem class:
@Entity@org.hibernate.annotation.Entity(dynamicInsert=true,dynamicUpdate=true)@Table(name="order_item")@IdClass(OrderItemPK.class)public class OrderItem implements Serializable{ private Product product; private Order order; private int quantity; @Id public Product getProduct(){ return product; } public void setProduct(Product product){ this.product=product; } @Id public Order getOrder(){ return order; } public void setOrder(Order order){ this.order=order; } @Column(name="quantity") public int getQuantity(){ return quantity; } public void setQuantity(int quantity){ this.quantity=quantity; }}
Product class:
@Entity@org.hibernate.annotation.Entity(dynamicInsert=true,dynamicUpdate=true)@Table(name="product")public class Product implements Serializable{ private int id; private String name; private double price; private Set<OrderItem> orderItems; @Id @GenericGenerator(name="g_id",strategy="increment") @GeneratedValue(generator="g_id") public int getId(){ return id; } public void setId(int id){ this.id=id; } public String getName(){ return name; } public void setName(String name){ this.name=name; } public double getPrice(){ return price; } public void setPrice(double price){ this.price=price; } @OneToMany(mappedBy="product") public Set<OrderItem> getOrderItems(){ return orderItems; } public void setOrderItems(Set<OrderItem> orderItems){ this.orderItems=orderItems; }}
Order class:
@Entity@org.hibernate.annotation.Entity(dynamicInsert=true,dynamicUpdate=true)@Table(name="tbl_order")public class Order implements Serializable{ private int id; private Calendar date; private Set<OrderItem> orderItems; @Id @GenericGenerator(name="g_id",strategy="increment") @GeneratedValue(generator="g_id") public int getId(){ return id; } public void setId(int id){ this.id=id; } public Calendar getDate(){ return date; } public void setDate(Calendar date){ this.date=date; } @OneToMany(mappedBy="order") public Set<OrderItem> getOrderItems(){ return orderItems; } public void setOrderItems(Set<OrderItem> orderItems){ this.orderItems=orderItems; }}