Merge branch 'master' of https://github.com/eugenp/tutorials into task/BAEL-9567
This commit is contained in:
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.hibernate.findall;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.Session;
|
||||
|
||||
import com.baeldung.hibernate.pojo.Student;
|
||||
|
||||
public class FindAll {
|
||||
|
||||
private Session session;
|
||||
|
||||
public FindAll(Session session) {
|
||||
super();
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
public List<Student> findAllWithJpql() {
|
||||
return session.createQuery("SELECT a FROM Student a", Student.class).getResultList();
|
||||
}
|
||||
|
||||
public List<Student> findAllWithCriteriaQuery() {
|
||||
CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
CriteriaQuery<Student> cq = cb.createQuery(Student.class);
|
||||
Root<Student> rootEntry = cq.from(Student.class);
|
||||
CriteriaQuery<Student> all = cq.select(rootEntry);
|
||||
TypedQuery<Student> allQuery = session.createQuery(all);
|
||||
return allQuery.getResultList();
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.hibernate.findall;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.hibernate.HibernateUtil;
|
||||
import com.baeldung.hibernate.pojo.Student;
|
||||
|
||||
public class FindAllUnitTest {
|
||||
|
||||
private Session session;
|
||||
private Transaction transaction;
|
||||
|
||||
private FindAll findAll;
|
||||
|
||||
@Before
|
||||
public void setUp() throws IOException {
|
||||
|
||||
session = HibernateUtil.getSessionFactory().openSession();
|
||||
transaction = session.beginTransaction();
|
||||
findAll = new FindAll(session);
|
||||
|
||||
session.createNativeQuery("delete from Student").executeUpdate();
|
||||
|
||||
Student student1 = new Student();
|
||||
session.persist(student1);
|
||||
|
||||
Student student2 = new Student();
|
||||
session.persist(student2);
|
||||
|
||||
Student student3 = new Student();
|
||||
session.persist(student3);
|
||||
|
||||
transaction.commit();
|
||||
transaction = session.beginTransaction();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
transaction.rollback();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCriteriaQuery_WhenFindAll_ThenGetAllPersons() {
|
||||
List<Student> list = findAll.findAllWithCriteriaQuery();
|
||||
assertEquals(3, list.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJpql_WhenFindAll_ThenGetAllPersons() {
|
||||
List<Student> list = findAll.findAllWithJpql();
|
||||
assertEquals(3, list.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-data-cassandra-reactive</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-data-cassandra-reactive</name>
|
||||
<description>Spring Data Cassandra reactive</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.0.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8
|
||||
</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-cassandra</artifactId>
|
||||
<version>2.1.2.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.cassandra.reactive;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringDataCassandraReactiveApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringDataCassandraReactiveApplication.class, args);
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.cassandra.reactive.controller;
|
||||
|
||||
import com.baeldung.cassandra.reactive.model.Employee;
|
||||
import com.baeldung.cassandra.reactive.service.EmployeeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("employee")
|
||||
public class EmployeeController {
|
||||
|
||||
@Autowired
|
||||
EmployeeService employeeService;
|
||||
|
||||
@PostConstruct
|
||||
public void saveEmployees() {
|
||||
List<Employee> employees = new ArrayList<>();
|
||||
employees.add(new Employee(123, "John Doe", "Delaware", "jdoe@xyz.com", 31));
|
||||
employees.add(new Employee(324, "Adam Smith", "North Carolina", "asmith@xyz.com", 43));
|
||||
employees.add(new Employee(355, "Kevin Dunner", "Virginia", "kdunner@xyz.com", 24));
|
||||
employees.add(new Employee(643, "Mike Lauren", "New York", "mlauren@xyz.com", 41));
|
||||
employeeService.initializeEmployees(employees);
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
public Flux<Employee> getAllEmployees() {
|
||||
Flux<Employee> employees = employeeService.getAllEmployees();
|
||||
return employees;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Mono<Employee> getEmployeeById(@PathVariable int id) {
|
||||
return employeeService.getEmployeeById(id);
|
||||
}
|
||||
|
||||
@GetMapping("/filterByAge/{age}")
|
||||
public Flux<Employee> getEmployeesFilterByAge(@PathVariable int age) {
|
||||
return employeeService.getEmployeesFilterByAge(age);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.cassandra.reactive.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
|
||||
import org.springframework.data.cassandra.core.mapping.Table;
|
||||
|
||||
@Table
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Employee {
|
||||
@PrimaryKey
|
||||
private int id;
|
||||
private String name;
|
||||
private String address;
|
||||
private String email;
|
||||
private int age;
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.cassandra.reactive.repository;
|
||||
|
||||
import com.baeldung.cassandra.reactive.model.Employee;
|
||||
import org.springframework.data.cassandra.repository.AllowFiltering;
|
||||
import org.springframework.data.cassandra.repository.ReactiveCassandraRepository;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
public interface EmployeeRepository extends ReactiveCassandraRepository<Employee, Integer> {
|
||||
@AllowFiltering
|
||||
Flux<Employee> findByAgeGreaterThan(int age);
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.cassandra.reactive.service;
|
||||
|
||||
import com.baeldung.cassandra.reactive.model.Employee;
|
||||
import com.baeldung.cassandra.reactive.repository.EmployeeRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class EmployeeService {
|
||||
|
||||
@Autowired
|
||||
EmployeeRepository employeeRepository;
|
||||
|
||||
public void initializeEmployees(List<Employee> employees) {
|
||||
Flux<Employee> savedEmployees = employeeRepository.saveAll(employees);
|
||||
savedEmployees.subscribe();
|
||||
}
|
||||
|
||||
public Flux<Employee> getAllEmployees() {
|
||||
Flux<Employee> employees = employeeRepository.findAll();
|
||||
return employees;
|
||||
}
|
||||
|
||||
public Flux<Employee> getEmployeesFilterByAge(int age) {
|
||||
return employeeRepository.findByAgeGreaterThan(age);
|
||||
}
|
||||
|
||||
public Mono<Employee> getEmployeeById(int id) {
|
||||
return employeeRepository.findById(id);
|
||||
}
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
spring.data.cassandra.keyspace-name=practice
|
||||
spring.data.cassandra.port=9042
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.cassandra.reactive;
|
||||
|
||||
import com.baeldung.cassandra.reactive.model.Employee;
|
||||
import com.baeldung.cassandra.reactive.repository.EmployeeRepository;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class ReactiveEmployeeRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
EmployeeRepository repository;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
Flux<Employee> deleteAndInsert = repository.deleteAll() //
|
||||
.thenMany(repository.saveAll(Flux.just(
|
||||
new Employee(111, "John Doe", "Delaware", "jdoe@xyz.com", 31),
|
||||
new Employee(222, "Adam Smith", "North Carolina", "asmith@xyz.com", 43),
|
||||
new Employee(333, "Kevin Dunner", "Virginia", "kdunner@xyz.com", 24),
|
||||
new Employee(444, "Mike Lauren", "New York", "mlauren@xyz.com", 41))));
|
||||
|
||||
StepVerifier.create(deleteAndInsert).expectNextCount(4).verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRecordsAreInserted_whenDbIsQueried_thenShouldIncludeNewRecords() {
|
||||
|
||||
Mono<Long> saveAndCount = repository.count()
|
||||
.doOnNext(System.out::println)
|
||||
.thenMany(repository.saveAll(Flux.just(new Employee(325, "Kim Jones", "Florida", "kjones@xyz.com", 42),
|
||||
new Employee(654, "Tom Moody", "New Hampshire", "tmoody@xyz.com", 44))))
|
||||
.last()
|
||||
.flatMap(v -> repository.count())
|
||||
.doOnNext(System.out::println);
|
||||
|
||||
StepVerifier.create(saveAndCount).expectNext(6L).verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAgeForFilter_whenDbIsQueried_thenShouldReturnFilteredRecords() {
|
||||
StepVerifier.create(repository.findByAgeGreaterThan(35)).expectNextCount(2).verifyComplete();
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.hibernate.onetoone;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
public class HibernateUtil {
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private static SessionFactory buildSessionFactory(Strategy strategy) {
|
||||
try {
|
||||
// Create the SessionFactory from hibernate-annotation.cfg.xml
|
||||
Configuration configuration = new Configuration();
|
||||
|
||||
for (Class<?> entityClass : strategy.getEntityClasses()) {
|
||||
configuration.addAnnotatedClass(entityClass);
|
||||
}
|
||||
configuration.configure("one-to-one.cfg.xml");
|
||||
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
|
||||
.build();
|
||||
|
||||
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||
|
||||
return sessionFactory;
|
||||
} catch (Throwable ex) {
|
||||
ex.printStackTrace();
|
||||
throw new ExceptionInInitializerError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static SessionFactory getSessionFactory(Strategy strategy) {
|
||||
if (sessionFactory == null)
|
||||
sessionFactory = buildSessionFactory(strategy);
|
||||
return sessionFactory;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.hibernate.onetoone;
|
||||
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public enum Strategy {
|
||||
//See that the classes belongs to different packages
|
||||
FOREIGN_KEY(Arrays.asList(com.baeldung.hibernate.onetoone.foreignkeybased.User.class,
|
||||
com.baeldung.hibernate.onetoone.foreignkeybased.Address.class)),
|
||||
SHARED_PRIMARY_KEY(Arrays.asList(com.baeldung.hibernate.onetoone.sharedkeybased.User.class,
|
||||
com.baeldung.hibernate.onetoone.sharedkeybased.Address.class)),
|
||||
JOIN_TABLE_BASED(Arrays.asList(com.baeldung.hibernate.onetoone.jointablebased.Employee.class,
|
||||
com.baeldung.hibernate.onetoone.jointablebased.WorkStation.class));
|
||||
|
||||
private List<Class<?>> entityClasses;
|
||||
|
||||
Strategy(List<Class<?>> entityClasses) {
|
||||
this.entityClasses = entityClasses;
|
||||
}
|
||||
|
||||
public List<Class<?>> getEntityClasses() {
|
||||
return entityClasses;
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.hibernate.onetoone.foreignkeybased;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "address")
|
||||
public class Address {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "street")
|
||||
private String street;
|
||||
|
||||
@Column(name = "city")
|
||||
private String city;
|
||||
|
||||
@OneToOne(mappedBy = "address")
|
||||
private User user;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.hibernate.onetoone.foreignkeybased;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "username")
|
||||
private String userName;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "address_id", referencedColumnName = "id")
|
||||
private Address address;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.hibernate.onetoone.jointablebased;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "employee")
|
||||
public class Employee {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "ename")
|
||||
private String name;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
@JoinTable(name = "emp_workstation", joinColumns = {@JoinColumn(name = "employee_id", referencedColumnName = "id")},
|
||||
inverseJoinColumns = {@JoinColumn(name = "workstation_id", referencedColumnName = "id")})
|
||||
private WorkStation workStation;
|
||||
|
||||
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 WorkStation getWorkStation() {
|
||||
return workStation;
|
||||
}
|
||||
|
||||
public void setWorkStation(WorkStation workStation) {
|
||||
this.workStation = workStation;
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.hibernate.onetoone.jointablebased;
|
||||
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "workstation")
|
||||
public class WorkStation {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "workstation_number")
|
||||
private Integer workstationNumber;
|
||||
|
||||
@Column(name = "floor")
|
||||
private String floor;
|
||||
|
||||
@OneToOne(mappedBy = "workStation")
|
||||
private Employee employee;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getWorkstationNumber() {
|
||||
return workstationNumber;
|
||||
}
|
||||
|
||||
public void setWorkstationNumber(Integer workstationNumber) {
|
||||
this.workstationNumber = workstationNumber;
|
||||
}
|
||||
|
||||
public String getFloor() {
|
||||
return floor;
|
||||
}
|
||||
|
||||
public void setFloor(String floor) {
|
||||
this.floor = floor;
|
||||
}
|
||||
|
||||
public Employee getEmployee() {
|
||||
return employee;
|
||||
}
|
||||
|
||||
public void setEmployee(Employee employee) {
|
||||
this.employee = employee;
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.hibernate.onetoone.sharedkeybased;
|
||||
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.MapsId;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "address")
|
||||
public class Address {
|
||||
|
||||
@Id
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "street")
|
||||
private String street;
|
||||
|
||||
@Column(name = "city")
|
||||
private String city;
|
||||
|
||||
@OneToOne
|
||||
@MapsId
|
||||
private User user;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.hibernate.onetoone.sharedkeybased;
|
||||
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "username")
|
||||
private String userName;
|
||||
|
||||
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
|
||||
private Address address;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
|
||||
<property name="hibernate.connection.password"></property>
|
||||
<property name="hibernate.connection.url">jdbc:h2:mem:spring_hibernate_one_to_one</property>
|
||||
<property name="hibernate.connection.username">sa</property>
|
||||
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
|
||||
<property name="hibernate.current_session_context_class">thread</property>
|
||||
<property name="hibernate.show_sql">true</property>
|
||||
<property name="hibernate.hbm2ddl.auto">create-drop</property>
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.baeldung.hibernate.onetoone;
|
||||
|
||||
import com.baeldung.hibernate.onetoone.foreignkeybased.Address;
|
||||
import com.baeldung.hibernate.onetoone.foreignkeybased.User;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class HibernateOneToOneAnnotationFKBasedIntegrationTest {
|
||||
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private Session session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
sessionFactory = HibernateUtil.getSessionFactory(Strategy.FOREIGN_KEY);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenData_whenInsert_thenCreates1to1relationship() {
|
||||
User user = new User();
|
||||
user.setUserName("alice@baeldung.com");
|
||||
|
||||
Address address = new Address();
|
||||
address.setStreet("FK Street");
|
||||
address.setCity("FK City");
|
||||
|
||||
address.setUser(user);
|
||||
user.setAddress(address);
|
||||
|
||||
//Address entry will automatically be created by hibernate, since cascade type is specified as ALL
|
||||
session.persist(user);
|
||||
session.getTransaction().commit();
|
||||
|
||||
assert1to1InsertedData();
|
||||
}
|
||||
|
||||
private void assert1to1InsertedData() {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<User> userList = session.createQuery("FROM User").list();
|
||||
|
||||
assertNotNull(userList);
|
||||
assertEquals(1, userList.size());
|
||||
|
||||
User user = userList.get(0);
|
||||
assertEquals("alice@baeldung.com", user.getUserName());
|
||||
|
||||
Address address = user.getAddress();
|
||||
assertNotNull(address);
|
||||
assertEquals("FK Street", address.getStreet());
|
||||
assertEquals("FK City", address.getCity());
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
session.close();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterTests() {
|
||||
sessionFactory.close();
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.baeldung.hibernate.onetoone;
|
||||
|
||||
import com.baeldung.hibernate.onetoone.jointablebased.Employee;
|
||||
import com.baeldung.hibernate.onetoone.jointablebased.WorkStation;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class HibernateOneToOneAnnotationJTBasedIntegrationTest {
|
||||
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private Session session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
sessionFactory = HibernateUtil.getSessionFactory(Strategy.JOIN_TABLE_BASED);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenData_whenInsert_thenCreates1to1relationship() {
|
||||
Employee employee = new Employee();
|
||||
employee.setName("bob@baeldung.com");
|
||||
|
||||
WorkStation workStation = new WorkStation();
|
||||
workStation.setWorkstationNumber(626);
|
||||
workStation.setFloor("Sixth Floor");
|
||||
|
||||
|
||||
employee.setWorkStation(workStation);
|
||||
workStation.setEmployee(employee);
|
||||
|
||||
session.persist(employee);
|
||||
session.getTransaction().commit();
|
||||
|
||||
assert1to1InsertedData();
|
||||
}
|
||||
|
||||
private void assert1to1InsertedData() {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Employee> employeeList = session.createQuery("FROM Employee").list();
|
||||
assertNotNull(employeeList);
|
||||
assertEquals(1, employeeList.size());
|
||||
|
||||
Employee employee = employeeList.get(0);
|
||||
assertEquals("bob@baeldung.com", employee.getName());
|
||||
|
||||
WorkStation workStation = employee.getWorkStation();
|
||||
|
||||
assertNotNull(workStation);
|
||||
assertEquals((long) 626, (long) workStation.getWorkstationNumber());
|
||||
assertEquals("Sixth Floor", workStation.getFloor());
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
session.close();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterTests() {
|
||||
sessionFactory.close();
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package com.baeldung.hibernate.onetoone;
|
||||
|
||||
import com.baeldung.hibernate.onetoone.sharedkeybased.Address;
|
||||
import com.baeldung.hibernate.onetoone.sharedkeybased.User;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class HibernateOneToOneAnnotationSPKBasedIntegrationTest {
|
||||
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private Session session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
sessionFactory = HibernateUtil.getSessionFactory(Strategy.SHARED_PRIMARY_KEY);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenData_whenInsert_thenCreates1to1relationship() {
|
||||
User user = new User();
|
||||
user.setUserName("alice@baeldung.com");
|
||||
|
||||
Address address = new Address();
|
||||
address.setStreet("SPK Street");
|
||||
address.setCity("SPK City");
|
||||
|
||||
address.setUser(user);
|
||||
user.setAddress(address);
|
||||
|
||||
//Address entry will automatically be created by hibernate, since cascade type is specified as ALL
|
||||
session.persist(user);
|
||||
session.getTransaction().commit();
|
||||
|
||||
assert1to1InsertedData();
|
||||
}
|
||||
|
||||
|
||||
private void assert1to1InsertedData() {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<User> userList = session.createQuery("FROM User").list();
|
||||
assertNotNull(userList);
|
||||
assertEquals(1, userList.size());
|
||||
|
||||
User user = userList.get(0);
|
||||
assertEquals("alice@baeldung.com", user.getUserName());
|
||||
|
||||
Address address = user.getAddress();
|
||||
assertNotNull(address);
|
||||
assertEquals("SPK Street", address.getStreet());
|
||||
assertEquals("SPK City", address.getCity());
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
session.close();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterTests() {
|
||||
sessionFactory.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
|
||||
<property name="hibernate.connection.password"></property>
|
||||
<property name="hibernate.connection.url">jdbc:h2:mem:spring_hibernate_one_to_one</property>
|
||||
<property name="hibernate.connection.username">sa</property>
|
||||
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
|
||||
<property name="hibernate.current_session_context_class">thread</property>
|
||||
<property name="hibernate.show_sql">true</property>
|
||||
<property name="hibernate.hbm2ddl.auto">create-drop</property>
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
||||
Reference in New Issue
Block a user