The CRUD of the link.

Source: Internet
Author: User

The CRUD of the link.

CRUD_Cascade_Fetch In Association
1. hibernate_1700_one2many_many2one_bi_crud
2. Set cascade to set the operations on the associated objects during persistence (CUD and R are Fetch tubes)
3. cascade only saves us the trouble of programming, so don't take its role too seriously.
A) the Cascade attribute specifies what operations are performed when the associated object is bound together.
B) Merge = save + update
C) refresh = the data in A that needs to be changed to B.
4. Tie law: bidirectional relationships must be set in the program.
5. Tie law: bidirectional mappedBy
6. fetch
A) iron law: do not set the eager on both sides of the two-way Interface (there will be extra query statements to issue)
B) be cautious when configuring fetch for multiple parties. In combination with specific applications, Lazy generally does not use eager. In special cases (when there are few Parties, you can consider this when improving efficiency)
7. O/R Mapping Programming Model
A) ing model
I. jpa annotation
Ii. hibernate annotation extension
Iii. hibernate xml
Iiii. jpa xml
B) Programming Interface
I. jpa Programming Interface
Ii. hibernate Programming Interface
C) Data Query Language
I. HQL
Ii. EJBQL (JPQL)
8. to delete or update an object, load the object first and get out of the specific ID.
9. If the association relationship is eliminated, the relationship is set to null first, and then the corresponding record is deleted. If no record is deleted, the record becomes junk data.
Link ing Summary
1. What kind of relationship, what kind of table, and what kind of ing?
5. CRUD, just follow the natural understanding (hands-on test)

 

For example, multiple users correspond to one Group)

User:

 1 package com.bjsxt.hibernate; 2  3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.JoinColumn; 7 import javax.persistence.ManyToOne; 8 import javax.persistence.Table; 9 10 @Entity11 @Table(name="t_user")12 public class User {13     private Integer id;14     15     private String name;16     17     private Group group;18     19     @Id20     @GeneratedValue21     public Integer getId() {22         return id;23     }24 25     public void setId(Integer id) {26         this.id = id;27     }28 29     public String getName() {30         return name;31     }32 33     public void setName(String name) {34         this.name = name;35     }36 37     @ManyToOne38     @JoinColumn(name="group_ID")39     public Group getGroup() {40         return group;41     }42 43     public void setGroup(Group group) {44         this.group = group;45     }46 }
View Code

Group:

 1 package com.bjsxt.hibernate; 2  3 import java.util.HashSet; 4 import java.util.Set; 5  6 import javax.persistence.Entity; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.Id; 9 import javax.persistence.OneToMany;10 import javax.persistence.Table;11 12 @Entity13 @Table(name="t_group")14 public class Group {15     private Integer id;16     17     private String name;18     19     private Set<User> users = new HashSet<User>();20 21     @Id22     @GeneratedValue23     public Integer getId() {24         return id;25     }26 27     public void setId(Integer id) {28         this.id = id;29     }30 31     public String getName() {32         return name;33     }34 35     public void setName(String name) {36         this.name = name;37     }38 39     @OneToMany(mappedBy="group")40     public Set<User> getUsers() {41         return users;42     }43 44     public void setUsers(Set<User> users) {45         this.users = users;46     }47 }
View Code

Save order: save one by one

 1 @Test 2     public void testSaveUser(){ 3         User u = new User(); 4         u.setName("u1"); 5         Group g = new Group(); 6         g.setName("g1"); 7         u.setGroup(g); 8         Session session = sf.getCurrentSession(); 9         session.beginTransaction();10         session.save(g);11         session.save(u);12         session.getTransaction().commit();13     }

If you want to save the u with g, you need to set Cascade in the User:

1    @ManyToOne(cascade=CascadeType.ALL)2     @JoinColumn(name="group_ID")3     public Group getGroup() {4         return group;5     }

Then save the Code:

 1     @Test 2     public void testSaveUser(){ 3         User u = new User(); 4         u.setName("u1"); 5         Group g = new Group(); 6         g.setName("g1"); 7         u.setGroup(g); 8         Session session = sf.getCurrentSession(); 9         session.beginTransaction();10 //        session.save(g);11         session.save(u);12         session.getTransaction().commit();13     }

The purpose of setting Casecade is to save cascade operations. If it is too troublesome, clear the relationships and save them one by one.

CRUD operation:

C: Create new;

R: Retrieve search;

U: Update;

D: Delete.

Cascade is responsible for CUD (addition, modification, and deletion), and fetch is responsible for R (retrieval ).

Think with a normal person's mind, and then verify through experiments.

When multiple users are queried, one Party (Group) is automatically taken out ):

The default value of onetovel is fetch = FetchType. LAZY.

The default value of ManyToOne is fetch = FetchType. EAGER.

    @Test    public void testGetUser(){        testSaveUser();                Session session = sf.getCurrentSession();        session.beginTransaction();        User u = (User) session.load(User.class, 1);        System.out.println(u.getGroup().getName());        session.getTransaction().commit();    }

One Party (Group) will not bring out multiple users, because this will lead to very serious performance problems. hibernate is also designed based on this idea:

    @Test    public void testGetGroup(){        testSaveUser();                Session session = sf.getCurrentSession();        session.beginTransaction();        Group g = (Group) session.get(Group.class, 1);        session.getTransaction().commit();        for(User u : g.getUsers()){            System.out.println(u.getName());        }

If you want to include multiple parties when querying one party, You need to configure the fetch annotation for one party:

FetchType has two values:

    @OneToMany(mappedBy="group",            cascade={CascadeType.ALL},            fetch=FetchType.EAGER    )    public Set<User> getUsers() {        return users;    }

In this way, hibernate will issue the SQL statement for joint queries.

If fetchType = FetchType. EAGER is set for many-to-one and one-to-many operations, sometimes a query row needs to be associated with many queries, which is not efficient. However, you sometimes need to query all the caches that place them in hibernate. This is based on your actual business needs.

Jar package link: https://pan.baidu.com/s/1eSf3G1k password: bpxb

Code link: https://pan.baidu.com/s/1cw6N5k password: 43im

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.