JPA mapping relationship

relationship:

Entity relationship is the relationship between the entity and the entity, classified as unidirectional and bidirectional association from the association direction, divided into one, one-to-many, etc. from the number of entities. For any two entities, we should distinguish these two aspects of the relationship between them.

One in two configurations:

  1: primary key sharing (the primary key of an entity is further configured to a primary key of the entity)

  2: foreign key with the unique

 

Many single

  : Configuring an entity's foreign key     database foreign keys exist parties, it is best to let parties to maintain the foreign keys , the party waiving management and maintenance

 

Bidirectional many / two-way-to-many

 

public class ProductDir {

 

  @Id

 

  @GeneratedValue

 

  private Long id;

 

  private String name;

 

  @OneToMany

 

  @JoinColumn(name="dir_id")

 

  private Set<Product> products = new HashSet<Product>();

 

  

 

@Entity

 

public class Product {

 

  @Id

 

  @GeneratedValue

 

  private Long id;

 

  private String name;

 

  @ManyToOne (fetch = FetchType.LAZY) // implement lazy loading

 

  @JoinColumn(name = "dir_id")

 

  private ProductDir dir;

Type collection mapping

  1.list PersistentBag ( ordered, can be repeated )

     2.set PersistentSet ( disorder not repeat )

 

cascade

    :

    Cascade save  PERSIST

    Cascade delete REMOVE

    All ALL

    Orphan delete orphanRemoval = true (from one to lift the relationship, the foreign key set to make multi-empty)

 

Single-many

    

  @ManyToMany

       Table disposed intermediate @joinTable

  joinColumns: The first column names inverseJoinColumns: the next column name

    Entity class no change default lazy loading configure only one entity class

Bi-many

      Column Teacher / Student

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/1999wang/p/11260386.html