Advertisement
paulito81

Java @Entity persist with @OneToOne problem

Nov 15th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.27 KB | None | 0 0
  1. Hi,
  2.  
  3. I have a problem with persist a new Account, when i am running my AccountIT test. The error i get is:
  4.  
  5. javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: no.phasfjo.dto.Customer
  6.  
  7. I figure it has to do something with the @OnToOne annotation - bu i cannot figure out how to change it correctly?
  8. Please do anyone see the problem ?
  9.  
  10.  
  11. --------
  12.  
  13.  
  14. @Entity
  15. @SequenceGenerator(name = "SEQ_ACC", initialValue = 50)
  16. public class Account {
  17.  
  18. @Id
  19. @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ACC")
  20. private int id;
  21.  
  22. @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)//, fetch = FetchType.EAGER)
  23. @JoinColumn(name = "FK_CUSTOMER")
  24. private Customer customer;
  25.  
  26. @OneToOne(cascade = CascadeType.ALL)
  27. @JoinColumn(name = "FK_LOGIN")
  28. private Login login;
  29.  
  30. public Account(Customer customer, Login login) {
  31. this.customer = customer;
  32. this.login = login;
  33. }
  34.  
  35. public Account() {
  36.  
  37. }
  38. public Customer getCustomer() {
  39. return customer;
  40. }
  41.  
  42. public int getId() {
  43. return id;
  44. }
  45.  
  46. public void setId(int id) {
  47. this.id = id;
  48. }
  49.  
  50. public void setCustomer(Customer customer) {
  51. this.customer = customer;
  52. }
  53.  
  54. public Login getLogin() {
  55. return login;
  56. }
  57.  
  58. public void setLogin(Login login) {
  59. this.login = login;
  60. }
  61.  
  62. @Override
  63. public String toString() {
  64. return "Account{" +
  65. "id=" + id +
  66. ", customer=" + customer +
  67. ", customer= '" + login +
  68. '}';
  69. }
  70. }
  71.  
  72. public class JpaAccountDao implements AccountDao {
  73.  
  74. @PersistenceContext(unitName = "account")
  75. private EntityManager entityManager;
  76.  
  77. public JpaAccountDao() {
  78. }
  79. public JpaAccountDao(EntityManager entityManager){
  80. this.entityManager = entityManager;
  81. }
  82.  
  83. @Override
  84. public Account persist(Account account) {
  85. if( account == null )
  86. throw new IllegalArgumentException("No account could be created!");
  87. entityManager.persist(account);
  88. return account;
  89. }
  90.  
  91. @Override
  92. public Boolean update(Account account) {
  93. if( !entityManager.contains(account)) {
  94. entityManager.merge(account);
  95. }
  96. return true;
  97. }
  98. }
  99.  
  100.  
  101. (Init.sql file)
  102.  
  103. INSERT INTO BOOK (id, title, price, description, number, instantiationDate) VALUES (1,'Mio min Mio', 100.0, 'Book about two brothers', '8-321389213', '2016-05-11 23:42:21');
  104. INSERT INTO BOOK (id, title, price, description, number, instantiationDate ) VALUES (2, 'Franks dagbok', 10.0, 'About the war and Auchwitch', '13-321321321', '2016-11-05 20:00:00' );
  105.  
  106. INSERT INTO CUSTOMER (FK_CUSTOMER, firstName, lastName, email, phoneNumber, birth) VALUES (1, 'Kim', 'Pedersen','kim@yahoo.no','90045870', '1980-11-05');
  107. INSERT INTO CUSTOMER (FK_CUSTOMER, firstName, lastName, email, phoneNumber, birth) VALUES (2, 'Silje', 'Kyrra','silje@yahoo.no','45236585', '1999-1-15');
  108.  
  109. INSERT INTO LOGIN (FK_LOGIN, username,password ) VALUES (1,'kimPedda', 'kimSimDimSum');
  110. INSERT INTO LOGIN (FK_LOGIN, username,password ) VALUES (2,'Silkyra', 'SanriKorraDigo');
  111.  
  112. INSERT INTO ACCOUNT (id, FK_CUSTOMER, FK_LOGIN ) VALUES (1, 1, 1 );
  113. INSERT INTO ACCOUNT (id, FK_CUSTOMER, FK_LOGIN ) VALUES (2, 2, 2 );
  114.  
  115. (Persistence.xml)
  116.  
  117. <?xml version="1.0" encoding="UTF-8" ?>
  118. <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  119. xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  120. version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
  121. <persistence-unit name="TEST" transaction-type="RESOURCE_LOCAL">
  122. <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
  123. <class>no.phasfjo.dto.Book</class>
  124. <class>no.phasfjo.dto.Customer</class>
  125. <class>no.phasfjo.dto.Account</class>
  126. <class>no.phasfjo.dto.Login</class>
  127. <properties>
  128. <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
  129. <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:TEST"/>
  130. <property name="javax.persistence.jdbc.user" value="sa"/>
  131. <property name="javax.persistence.jdbc.password" value=""/>
  132. <property name="dialect" value="org.hibernate.dialect.H2Dialect"/>
  133. <property name="hibernate.hbm2ddl.auto" value="create"/>
  134. <property name="hibernate.hbm2ddl.import_files" value="init.sql"/>
  135. <property name="hibernate.show_sql" value="true"/>
  136. </properties>
  137. </persistence-unit>
  138. </persistence>
  139.  
  140.  
  141. public class AccountServiceIT {
  142.  
  143. private EntityManager entityManager;
  144. private EntityManagerFactory factory;
  145. private CustomerTestCase customerTestCase;
  146. private JpaAccountDao jpaAccountDao;
  147. private JpaCustomerDao jpaCustomerDao;
  148. private LoginTestCase loginTestCase;
  149. private JpaLoginDao jpaLoginDao;
  150. private Account account;
  151.  
  152. @Before
  153. public void setup() throws Exception {
  154. factory = Persistence.createEntityManagerFactory("TEST");
  155. entityManager = factory.createEntityManager();
  156. jpaAccountDao = new JpaAccountDao(entityManager);
  157. account = new Account();
  158. entityManager.getTransaction().begin();
  159. entityManager.getTransaction().commit();
  160. customerTestCase = new CustomerTestCase();
  161. loginTestCase = new LoginTestCase();
  162.  
  163. }
  164. @After
  165. public void tearDown() throws Exception {
  166. entityManager.close();
  167. factory.close();
  168. }
  169.  
  170. //TODO
  171. @Test
  172. public void persistNewAccountTest() throws Exception {
  173. Date date = new Date("08/05/1999 20:00:00");
  174. Customer customer = new Customer("Per","karstain","per@gmail.com", "90032122", date);
  175. jpaCustomerDao = customerTestCase.setup();
  176.  
  177. entityManager.getTransaction().begin();
  178. jpaCustomerDao.persist(customer);
  179. entityManager.getTransaction().commit();
  180.  
  181. Login login = new Login("perKarsten", "perre94134");
  182. jpaLoginDao = loginTestCase.setup();
  183. entityManager.getTransaction().begin();
  184. jpaLoginDao.persist(login);
  185. entityManager.getTransaction().commit();
  186.  
  187. account = new Account(customer, login);
  188. entityManager.getTransaction().begin();
  189. Account result = jpaAccountDao.persist(account);
  190. entityManager.getTransaction().commit();
  191.  
  192. assertTrue(result.getId() > 0);
  193.  
  194. }
  195. }
  196. public class CustomerTestCase {
  197.  
  198. private EntityManager entityManager;
  199. private EntityManagerFactory factory;
  200. private JpaCustomerDao jpaCustomerDao;
  201.  
  202. public CustomerTestCase() {
  203.  
  204. }
  205.  
  206. @Before
  207. public JpaCustomerDao setup() throws Exception {
  208. factory = Persistence.createEntityManagerFactory("TEST");
  209. entityManager = factory.createEntityManager();
  210. jpaCustomerDao = new JpaCustomerDao(entityManager);
  211.  
  212. return jpaCustomerDao;
  213. }
  214.  
  215. @After
  216. public void tearDown() throws Exception {
  217.  
  218. entityManager.close();
  219. factory.close();
  220. }
  221.  
  222.  
  223. public JpaCustomerDao getJpaCustomerDao() {
  224. return jpaCustomerDao;
  225. }
  226.  
  227. public void setJpaCustomerDao(JpaCustomerDao jpaCustomerDao) {
  228. this.jpaCustomerDao = jpaCustomerDao;
  229. }
  230. }
  231.  
  232. public class LoginTestCase {
  233.  
  234. private EntityManager entityManager;
  235. private EntityManagerFactory factory;
  236. private JpaLoginDao jpaLoginDao;
  237.  
  238. public LoginTestCase() {
  239.  
  240. }
  241.  
  242. @Before
  243. public JpaLoginDao setup() throws Exception {
  244. factory = Persistence.createEntityManagerFactory("TEST");
  245. entityManager = factory.createEntityManager();
  246. jpaLoginDao = new JpaLoginDao(entityManager);
  247.  
  248. return jpaLoginDao;
  249. }
  250.  
  251. @After
  252. public void tearDown() throws Exception {
  253. entityManager.close();
  254. factory.close();
  255. }
  256.  
  257.  
  258. public JpaLoginDao getJpaLoginDao() {
  259. return jpaLoginDao;
  260. }
  261.  
  262. public void setJpaLoginDao(JpaLoginDao jpaLoginDao) {
  263. this.jpaLoginDao = jpaLoginDao;
  264. }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement