Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hi,
- I have a problem with persist a new Account, when i am running my AccountIT test. The error i get is:
- javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: no.phasfjo.dto.Customer
- I figure it has to do something with the @OnToOne annotation - bu i cannot figure out how to change it correctly?
- Please do anyone see the problem ?
- --------
- @Entity
- @SequenceGenerator(name = "SEQ_ACC", initialValue = 50)
- public class Account {
- @Id
- @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ACC")
- private int id;
- @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)//, fetch = FetchType.EAGER)
- @JoinColumn(name = "FK_CUSTOMER")
- private Customer customer;
- @OneToOne(cascade = CascadeType.ALL)
- @JoinColumn(name = "FK_LOGIN")
- private Login login;
- public Account(Customer customer, Login login) {
- this.customer = customer;
- this.login = login;
- }
- public Account() {
- }
- public Customer getCustomer() {
- return customer;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public void setCustomer(Customer customer) {
- this.customer = customer;
- }
- public Login getLogin() {
- return login;
- }
- public void setLogin(Login login) {
- this.login = login;
- }
- @Override
- public String toString() {
- return "Account{" +
- "id=" + id +
- ", customer=" + customer +
- ", customer= '" + login +
- '}';
- }
- }
- public class JpaAccountDao implements AccountDao {
- @PersistenceContext(unitName = "account")
- private EntityManager entityManager;
- public JpaAccountDao() {
- }
- public JpaAccountDao(EntityManager entityManager){
- this.entityManager = entityManager;
- }
- @Override
- public Account persist(Account account) {
- if( account == null )
- throw new IllegalArgumentException("No account could be created!");
- entityManager.persist(account);
- return account;
- }
- @Override
- public Boolean update(Account account) {
- if( !entityManager.contains(account)) {
- entityManager.merge(account);
- }
- return true;
- }
- }
- (Init.sql file)
- 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');
- 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' );
- INSERT INTO CUSTOMER (FK_CUSTOMER, firstName, lastName, email, phoneNumber, birth) VALUES (1, 'Kim', 'Pedersen','kim@yahoo.no','90045870', '1980-11-05');
- INSERT INTO CUSTOMER (FK_CUSTOMER, firstName, lastName, email, phoneNumber, birth) VALUES (2, 'Silje', 'Kyrra','silje@yahoo.no','45236585', '1999-1-15');
- INSERT INTO LOGIN (FK_LOGIN, username,password ) VALUES (1,'kimPedda', 'kimSimDimSum');
- INSERT INTO LOGIN (FK_LOGIN, username,password ) VALUES (2,'Silkyra', 'SanriKorraDigo');
- INSERT INTO ACCOUNT (id, FK_CUSTOMER, FK_LOGIN ) VALUES (1, 1, 1 );
- INSERT INTO ACCOUNT (id, FK_CUSTOMER, FK_LOGIN ) VALUES (2, 2, 2 );
- (Persistence.xml)
- <?xml version="1.0" encoding="UTF-8" ?>
- <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
- version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
- <persistence-unit name="TEST" transaction-type="RESOURCE_LOCAL">
- <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
- <class>no.phasfjo.dto.Book</class>
- <class>no.phasfjo.dto.Customer</class>
- <class>no.phasfjo.dto.Account</class>
- <class>no.phasfjo.dto.Login</class>
- <properties>
- <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
- <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:TEST"/>
- <property name="javax.persistence.jdbc.user" value="sa"/>
- <property name="javax.persistence.jdbc.password" value=""/>
- <property name="dialect" value="org.hibernate.dialect.H2Dialect"/>
- <property name="hibernate.hbm2ddl.auto" value="create"/>
- <property name="hibernate.hbm2ddl.import_files" value="init.sql"/>
- <property name="hibernate.show_sql" value="true"/>
- </properties>
- </persistence-unit>
- </persistence>
- public class AccountServiceIT {
- private EntityManager entityManager;
- private EntityManagerFactory factory;
- private CustomerTestCase customerTestCase;
- private JpaAccountDao jpaAccountDao;
- private JpaCustomerDao jpaCustomerDao;
- private LoginTestCase loginTestCase;
- private JpaLoginDao jpaLoginDao;
- private Account account;
- @Before
- public void setup() throws Exception {
- factory = Persistence.createEntityManagerFactory("TEST");
- entityManager = factory.createEntityManager();
- jpaAccountDao = new JpaAccountDao(entityManager);
- account = new Account();
- entityManager.getTransaction().begin();
- entityManager.getTransaction().commit();
- customerTestCase = new CustomerTestCase();
- loginTestCase = new LoginTestCase();
- }
- @After
- public void tearDown() throws Exception {
- entityManager.close();
- factory.close();
- }
- //TODO
- @Test
- public void persistNewAccountTest() throws Exception {
- Date date = new Date("08/05/1999 20:00:00");
- Customer customer = new Customer("Per","karstain","per@gmail.com", "90032122", date);
- jpaCustomerDao = customerTestCase.setup();
- entityManager.getTransaction().begin();
- jpaCustomerDao.persist(customer);
- entityManager.getTransaction().commit();
- Login login = new Login("perKarsten", "perre94134");
- jpaLoginDao = loginTestCase.setup();
- entityManager.getTransaction().begin();
- jpaLoginDao.persist(login);
- entityManager.getTransaction().commit();
- account = new Account(customer, login);
- entityManager.getTransaction().begin();
- Account result = jpaAccountDao.persist(account);
- entityManager.getTransaction().commit();
- assertTrue(result.getId() > 0);
- }
- }
- public class CustomerTestCase {
- private EntityManager entityManager;
- private EntityManagerFactory factory;
- private JpaCustomerDao jpaCustomerDao;
- public CustomerTestCase() {
- }
- @Before
- public JpaCustomerDao setup() throws Exception {
- factory = Persistence.createEntityManagerFactory("TEST");
- entityManager = factory.createEntityManager();
- jpaCustomerDao = new JpaCustomerDao(entityManager);
- return jpaCustomerDao;
- }
- @After
- public void tearDown() throws Exception {
- entityManager.close();
- factory.close();
- }
- public JpaCustomerDao getJpaCustomerDao() {
- return jpaCustomerDao;
- }
- public void setJpaCustomerDao(JpaCustomerDao jpaCustomerDao) {
- this.jpaCustomerDao = jpaCustomerDao;
- }
- }
- public class LoginTestCase {
- private EntityManager entityManager;
- private EntityManagerFactory factory;
- private JpaLoginDao jpaLoginDao;
- public LoginTestCase() {
- }
- @Before
- public JpaLoginDao setup() throws Exception {
- factory = Persistence.createEntityManagerFactory("TEST");
- entityManager = factory.createEntityManager();
- jpaLoginDao = new JpaLoginDao(entityManager);
- return jpaLoginDao;
- }
- @After
- public void tearDown() throws Exception {
- entityManager.close();
- factory.close();
- }
- public JpaLoginDao getJpaLoginDao() {
- return jpaLoginDao;
- }
- public void setJpaLoginDao(JpaLoginDao jpaLoginDao) {
- this.jpaLoginDao = jpaLoginDao;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement