PR-BAEL-2816 (#7733)
* implement hexagonal architecture pattern * Add source code for article BAEL-2463 * add source code for article BAEL-2816 * remove irrelevant code * Remove unnecessary default constructors * Refactor equals and hashCode methods in Java 7+ way * Remove equals and hashCode implementations * Change test methods conventional order * change saveRelation method to private To make the code more clean and readable
This commit is contained in:
parent
c2b029fd85
commit
c73001019c
|
@ -0,0 +1,56 @@
|
|||
package com.baeldung.hibernate.wherejointable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
|
||||
@Entity(name = "e_group")
|
||||
public class Group {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToMany(mappedBy = "groups")
|
||||
private List<User> users = new ArrayList<>();
|
||||
|
||||
public Group(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<User> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public void setUsers(List<User> users) {
|
||||
this.users = users;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Group [name=" + name + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
package com.baeldung.hibernate.wherejointable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
|
||||
import org.hibernate.annotations.WhereJoinTable;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToMany
|
||||
@JoinTable(name = "r_user_group", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "group_id"))
|
||||
private List<Group> groups = new ArrayList<>();
|
||||
|
||||
@WhereJoinTable(clause = "role='MODERATOR'")
|
||||
@ManyToMany
|
||||
@JoinTable(name = "r_user_group", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "group_id"))
|
||||
private List<Group> moderatorGroups = new ArrayList<>();
|
||||
|
||||
public User(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<Group> getGroups() {
|
||||
return groups;
|
||||
}
|
||||
|
||||
public void setGroups(List<Group> groups) {
|
||||
this.groups = groups;
|
||||
}
|
||||
|
||||
public void setModeratorGroups(List<Group> moderatorGroups) {
|
||||
this.moderatorGroups = moderatorGroups;
|
||||
}
|
||||
|
||||
public List<Group> getModeratorGroups() {
|
||||
return moderatorGroups;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User [name=" + name + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.hibernate.wherejointable;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity(name = "r_user_group")
|
||||
public class UserGroupRelation implements Serializable {
|
||||
|
||||
@Id
|
||||
@Column(name = "user_id", insertable = false, updatable = false)
|
||||
private final Long userId;
|
||||
|
||||
@Id
|
||||
@Column(name = "group_id", insertable = false, updatable = false)
|
||||
private final Long groupId;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private final UserGroupRole role;
|
||||
|
||||
public UserGroupRelation(Long userId, Long groupId, UserGroupRole role) {
|
||||
this.userId = userId;
|
||||
this.groupId = groupId;
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.hibernate.wherejointable;
|
||||
|
||||
public enum UserGroupRole {
|
||||
|
||||
MEMBER, MODERATOR
|
||||
|
||||
}
|
|
@ -0,0 +1,118 @@
|
|||
package com.baeldung.hibernate.wherejointable;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.dialect.H2Dialect;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HibernateWhereJoinTableIntegrationTest {
|
||||
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private Session session;
|
||||
|
||||
/**
|
||||
* Test data
|
||||
*/
|
||||
private User user1;
|
||||
private User user2;
|
||||
private User user3;
|
||||
private Group group1;
|
||||
private Group group2;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
Configuration configuration = new Configuration().addAnnotatedClass(User.class)
|
||||
.addAnnotatedClass(Group.class)
|
||||
.addAnnotatedClass(UserGroupRelation.class)
|
||||
.setProperty("hibernate.dialect", H2Dialect.class.getName())
|
||||
.setProperty("hibernate.connection.driver_class", org.h2.Driver.class.getName())
|
||||
.setProperty("hibernate.connection.url", "jdbc:h2:mem:test")
|
||||
.setProperty("hibernate.connection.username", "sa")
|
||||
.setProperty("hibernate.connection.password", "")
|
||||
.setProperty("hibernate.hbm2ddl.auto", "update");
|
||||
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
|
||||
.applySettings(configuration.getProperties())
|
||||
.build();
|
||||
|
||||
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
|
||||
user1 = new User("user1");
|
||||
user2 = new User("user2");
|
||||
user3 = new User("user3");
|
||||
|
||||
group1 = new Group("group1");
|
||||
group2 = new Group("group2");
|
||||
|
||||
session.save(group1);
|
||||
session.save(group2);
|
||||
|
||||
session.save(user1);
|
||||
session.save(user2);
|
||||
session.save(user3);
|
||||
|
||||
saveRelation(user1, group1, UserGroupRole.MODERATOR);
|
||||
saveRelation(user2, group1, UserGroupRole.MODERATOR);
|
||||
saveRelation(user3, group1, UserGroupRole.MEMBER);
|
||||
|
||||
saveRelation(user1, group2, UserGroupRole.MEMBER);
|
||||
saveRelation(user2, group2, UserGroupRole.MODERATOR);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterTests() {
|
||||
sessionFactory.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUser1_getGroups_returnsAllGroups() {
|
||||
List<Group> groups = user1.getGroups();
|
||||
assertEquals(2, groups.size());
|
||||
|
||||
assertTrue(groups.contains(group1));
|
||||
assertTrue(groups.contains(group2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUser1_getModeratorGroups_returnsOnlyModeratorGroups() {
|
||||
List<Group> groups = user1.getModeratorGroups();
|
||||
assertEquals(1, groups.size());
|
||||
|
||||
assertTrue(groups.contains(group1));
|
||||
}
|
||||
|
||||
private void saveRelation(User user, Group group, UserGroupRole role) {
|
||||
UserGroupRelation relation = new UserGroupRelation(user.getId(), group.getId(), role);
|
||||
|
||||
session.save(relation);
|
||||
session.flush();
|
||||
session.refresh(user);
|
||||
session.refresh(group);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue