add possession relationship

This commit is contained in:
DOHA 2016-09-05 19:47:58 +02:00
parent 230a3054ad
commit b62b5a37ab
4 changed files with 125 additions and 3 deletions

View File

@ -0,0 +1,8 @@
package org.baeldung.persistence.multiple.dao.user;
import org.baeldung.persistence.multiple.model.user.Possession;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PossessionRepository extends JpaRepository<Possession, Long> {
}

View File

@ -0,0 +1,86 @@
package org.baeldung.persistence.multiple.model.user;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(schema = "spring_jpa_user")
public class Possession {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
public Possession() {
super();
}
public Possession(final String name) {
super();
this.name = name;
}
public long getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = (prime * result) + (int) (id ^ (id >>> 32));
result = (prime * result) + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Possession other = (Possession) obj;
if (id != other.id) {
return false;
}
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
return true;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Possesion [id=").append(id).append(", name=").append(name).append("]");
return builder.toString();
}
}

View File

@ -1,10 +1,13 @@
package org.baeldung.persistence.multiple.model.user;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@ -22,6 +25,9 @@ public class User {
private int age;
@OneToMany
List<Possession> possessionList;
public User() {
super();
}
@ -58,6 +64,14 @@ public class User {
this.age = age;
}
public List<Possession> getPossessionList() {
return possessionList;
}
public void setPossessionList(List<Possession> possessionList) {
this.possessionList = possessionList;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();

View File

@ -1,14 +1,18 @@
package org.baeldung.persistence.service;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.Arrays;
import org.baeldung.config.ProductConfig;
import org.baeldung.config.UserConfig;
import org.baeldung.persistence.multiple.dao.product.ProductRepository;
import org.baeldung.persistence.multiple.dao.user.PossessionRepository;
import org.baeldung.persistence.multiple.dao.user.UserRepository;
import org.baeldung.persistence.multiple.model.product.Product;
import org.baeldung.persistence.multiple.model.user.Possession;
import org.baeldung.persistence.multiple.model.user.User;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -16,15 +20,20 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { UserConfig.class, ProductConfig.class })
@EnableTransactionManagement
public class JpaMultipleDBIntegrationTest {
@Autowired
private UserRepository userRepository;
@Autowired
private PossessionRepository possessionRepository;
@Autowired
private ProductRepository productRepository;
@ -37,9 +46,14 @@ public class JpaMultipleDBIntegrationTest {
user.setName("John");
user.setEmail("john@test.com");
user.setAge(20);
Possession p = new Possession("sample");
p = possessionRepository.save(p);
user.setPossessionList(Arrays.asList(p));
user = userRepository.save(user);
assertNotNull(userRepository.findOne(user.getId()));
final User result = userRepository.findOne(user.getId());
assertNotNull(result);
System.out.println(result.getPossessionList());
assertTrue(result.getPossessionList().size() == 1);
}
@Test