Relationships between tables Table 1 ...
One: a record corresponding to each recording two B A and B in Table A Table
: One relationship implementations:
1: foreign key
: Scene: Using the Customer table and expand the customer table
: Add a @OneToOne representative of a property in relation to a field added @JoinColumn name stored in the foreign key field name attribute referencedColumnNm other table (may be omitted)
2: Use the associated master key, the same primary key of two tables A B.
: Use annotations @PrimaryKeyJoinColumn not need to configure both attributes need to be added
Many: each recording two B A Table A corresponding plurality of records recorded in Table B Table B corresponding to a plurality of records in the table A
: Scene: a client-to-many correspondence between a client and a plurality of contacts contacts
: Unilateral: Add a plurality of recording attribute information should be one of a set of attribute hashset 2 was added in an annotation @OneToMany 3 must be more than one foreign key record reference to one of a primary key
: Multi: 1 @ManyToOne add annotations in the annotation attribute 2 is added on the property @joinColumn
Many to many: each recording two tables A A B Table B corresponding to each record in the table is recorded in Table B corresponding to the plurality of the plurality of records in the table A
: The role of the relationship between the user and the user application scenarios, for example the role of CRM (customer relationship management)
Many-to-use configuration annotations @ManyToMany
a set add a property entity
b ManyToMany add annotations on property
c: @JoinTable comment configuration relationship
Title intermediate table name
The current mapping table and the middle of the table
Tables and other relationships among mapping table
Many relationship can only give up the other party maintaining the correlation between maintenance
Configuring cascade operation
@ManyToMnay annotation cascade configuration properties
Related inquiries
1 way to use heavy and difficult to navigate query
"Object Property" query
For more queries default is lazy loading add comments @Transactional
In OneToMany need to add annotation attribute fetch
Value: FetchType.LAZY delay loading
FetchType.EAGER timely loading
Contact inquiries by customers
The default is to query a timely loading
You may be configured to delay loading
Realization: Simple Case:
1 .... pom.xml and applicationContext.xml See Table associated with many tables associated with one table
2 .... entity class
User class
1 package com.wsc.core.entity; 2 3 import javax.persistence.*; 4 import java.io.Serializable; 5 import java.util.HashSet; 6 import java.util.Set; 7 8 /** 9 * @version 1.0 10 * @ClassName User 11 * @Description TODO 12 * @Author WSC 13 * @Date 2019/8/21 9:55 14 **/ 15 @Entity 16 @Table(name="user") 17 public classThe implements the Serializable {the User // implement serialization 18 is @Id . 19 @GeneratedValue (Strategy = GenerationType.IDENTITY) 20 is the @Column (name = " user_id " ) 21 is Private Long the userId; 22 is the @Column (name = " username " ) 23 is Private String username; 24 the @Column (name = " password " ) 25 Private String password; 26 is // many association relationship cascade configuration attributes 27 @FetchType.LAZY delay loading 28 // FetchType.EAGER timely loading 29 @ManyToMany (= CascadeType.ALL in Cascade, FETCH = FetchType.EAGER) 30 // annotation configuration relationship 31 is // name the name of the intermediate table 32 // joinColumns this table and the mapping relationship between the intermediate table 33 is // inverseJoinColumns other intermediate table and the mapping table 34 is 35 @JoinTable (name = " user_role " , joinColumns @JoinColumn = (name = " the userid " , the referencedColumnName = " user_id " ), 36 inverseJoinColumns = @JoinColumn (name ="roleid",referencedColumnName = "role_id")) 37 Set<Role> role=new HashSet<Role>(); 38 39 @Override 40 public String toString() { 41 return "User{" + 42 "userId=" + userId + 43 ", username='" + username + '\'' + 44 ", password='" + password + '\'' + 45 ", role=" + role + 46 '}'; 47 } 48 49 public long getUserId() { 50 return userId; 51 } 52 53 public void setUserId(long userId) { 54 this.userId = userId; 55 } 56 57 public String getUsername() { 58 return username; 59 } 60 61 public void setUsername (String user) { 62 this .username = user; 63 } 64 65 public String getPassword () { 66 return password; 67 } 68 69 public void setPassword (String password) { 70 this .password = password; 71 } 72 73 public Set <Role> getRole () { 74 return role; 75 } 76 77 public void setRole(Set<Role> role) { 78 this.role = role; 79 } 80 }
Role category
1 package com.wsc.core.entity; 2 3 import javax.persistence.*; 4 import java.io.Serializable; 5 import java.util.HashSet; 6 import java.util.Set; 7 8 /** 9 * @version 1.0 10 * @ClassName Role 11 * @Description TODO 12 * @Author WSC 13 * @Date 2019/8/21 9:55 14 **/ 15 @Entity 16 @Table (name ="role") 17 public class Role the implements the Serializable { 18 is @GeneratedValue (Strategy = GenerationType.IDENTITY) . 19 @Id 20 is the @Column (name = "role_id" ) 21 is Private Long roleId; 22 is the @Column (name = "ROLE_NAME" ) 23 is Private String rolename; 24 @ the Column (name = "Memo" ) 25 Private String Memo; 26 is // many association relationship cascade configuration attribute 27 // to FetchType.LAZY delay loading 28 // FetchType.EAGER timely loading 29 @ManyToMany(mappedBy = "role",fetch = FetchType.EAGER) 30 Set<User> user=new HashSet<User>(); 31 32 public long getRoleId() { 33 return roleId; 34 } 35 36 public void setRoleId(long roleId) { 37 this.roleId = roleId; 38 } 39 40 public String getRolename() { 41 return rolename; 42 } 43 44 public void setRolename(String rolename) { 45 this.rolename = rolename; 46 } 47 48 public String getMemo() { 49 return memo; 50 } 51 52 public void setMemo(String memo) { 53 this.memo = memo; 54 } 55 56 public Set<User> getUser() { 57 return user; 58 } 59 60 public void setUser(Set<User> user) { 61 this.user = user; 62 } 63 64 @Override 65 public String toString() { 66 return "Role{" + 67 "roleId=" + roleId + 68 ", rolename='" + rolename + '\'' + 69 ", memo='" + memo + '\'' + 70 ", user=" + user + 71 '}'; 72 } 73 }
3 .... interfaces dao
UserDao class
1 package com.wsc.core.dao; 2 3 import com.wsc.core.entity.User; 4 import org.springframework.data.jpa.repository.JpaRepository; 5 6 public interface UserDao extends JpaRepository<User,Long> { 7 }
RoleDao class
1 package com.wsc.core.dao; 2 3 import com.wsc.core.entity.Role; 4 import org.springframework.data.jpa.repository.JpaRepository; 5 6 public interface RoleDao extends JpaRepository<Role,Long> { 7 }
test category
1 package com.wsc.core.onetone; 2 3 import com.wsc.core.dao.RoleDao; 4 import com.wsc.core.dao.UserDao; 5 import com.wsc.core.entity.Role; 6 import com.wsc.core.entity.User; 7 import org.junit.Test; 8 import org.junit.runner.RunWith; 9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.test.annotation.Commit; 11 import org.springframework.test.context.ContextConfiguration; 12 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 import org.springframework.transaction.annotation.Transactional; 14 15 /** 16 * @version 1.0 17 * @ClassName ManyToMany 18 * @Description TODO 19 * @Author WSC 20 * @Date 2019/8/21 10:12 21 **/ 22 @RunWith(SpringJUnit4ClassRunner.class) 23 @ContextConfiguration(locations = "classpath:applicationContext.xml") 24 public class ManyToMany { 25 @Autowired 26 private UserDao userDao; 27 @Autowired 28 private RoleDao roleDao; 29 @Transactional //事务 30 @Commit //提交 31 @Test 32 public void testUserAndRole(){ 33 //对象user 34 User user = new User(); 35 user.setUsername("admin"); 36 user.setPassword("123456"); 37 //对象role 38 Role = Role new new Role (); 39 role.setRolename ( "administrator" ); 40 role.setMemo ( "full control" ); 41 is 42 is // 3 arrangement relationship between the users and roles 43 is user.getRole (). the Add (role); 44 is role.getUser () the Add (user);. 45 46 is // . 4 can put the user data is written character 47 userDao.save (user); 48 // roleDao.save (role); 49 } 50 @Test 51 is @Transactional 52 is @Commit 53 is public void Test02 () { 54 User user = new User(); 55 user.setUsername("迪丽热巴"); 56 user.setPassword("123"); 57 58 User user1 = new User(); 59 user1.setUsername("宝宝"); 60 user1.setPassword("12346"); 61 62 Role role = new Role(); 63 role.setRolename("演员"); 64 65 Role role1 = new Role(); 66 role1.setRolename("导演"); 67 68 69 @ 3 Relationship between users and roles configuration 70 user.getRole () the Add (Role);. 71 is . User.getRole () the Add (role1); 72 user1.getRole () the Add (Role).; 73 is user1.getRole () the Add (role1);. 74 . role.getUser () the Add (User); 75 . role.getUser () the Add (user1); 76 . role1.getUser () the Add (User); 77 role1 . .getUser () the Add (user1); 78 // . 4 can put the user data is written character 79 userDao.save (user); 80 userDao.save (user1); 81 82 } 83 84 @Transactional 85 @Commit 86 @Test 87 public void deleteUser(){ 88 userDao.delete(2l); 89 } 90 }