Reputation: 42
Hi i having problem with deleting of an entity. The entitymanager would not remove the entity. Do anyone see the error in the code?
Error message:
java.lang.AssertionError: Expected :null Actual :Account{id=1, customer=Customer{customerId=1, firstName='Kim', lastName='Pedersen', email='[email protected]', phoneNumber='90045870', birth=1980-11-05 00:00:00.0}, login= 'Login{Id=1, username='kimPedda', password='kimSimDimSum'}}
@Entity
@NamedQuery(name = "Account.getAll", query = "select a from Account a")
@SequenceGenerator(name = "SEQ_ACC", initialValue = 50)
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ACC")
private int id;
@OneToOne(cascade = CascadeType.ALL)//, fetch = FetchType.EAGER)
@JoinColumn(name = "FK_CUSTOMER")
private Customer customer;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "FK_LOGIN")
private Login login;
/*
-------------------------------------------
CONSTRUCTORS
-------------------------------------------
*/
public Account(Customer customer, Login login) {
this.customer = customer;
this.login = login;
}
public Account() {
}
// ======================================
// = GET AND SET =
// ======================================
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;
}
// ======================================
// = TO STRING =
// ======================================
@Override
public String toString() {
return "Account{" +
"id=" + id +
", customer=" + customer +
", login= '" + 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 delete(int id) {
if( id != 0) {
Account account = entityManager.find(Account.class,id);
entityManager.remove(account);
return true;
}
throw new IllegalArgumentException(String.format("Account with id-nr:{%d] could not be deleted =C ", id ));
}
@Override
public Account findById(int id) {
if( id <= 0 )
throw new IllegalArgumentException("No id was found!");
return entityManager.find(Account.class, id);
}
@Override
public List<Account> getAll() {
TypedQuery<Account> query = entityManager.createNamedQuery("Account.getAll", Account.class);
return query.getResultList();
}
}
public class AccountServiceIT {
private EntityManager entityManager;
private EntityManagerFactory factory;
private JpaAccountDao jpaAccountDao;
private JpaCustomerDao jpaCustomerDao;
private CustomerTestCase customerTestCase;
private JpaLoginDao jpaLoginDao;
private Account account;
private Account account2;
@Before
public void setup() throws Exception {
factory = Persistence.createEntityManagerFactory("TEST");
entityManager = factory.createEntityManager();
jpaAccountDao = new JpaAccountDao(entityManager);
account = new Account();
account2 = new Account();
}
@After
public void tearDown() throws Exception {
entityManager.close();
factory.close();
}
/*
Delete a account popularized via the init.script
*/
// TODO CREATE A TESTE THATS RUNS
@Test
public void deleteAccountTest() throws Exception {
Account account = entityManager.find(Account.class, 1);
entityManager.getTransaction().begin();
boolean result = jpaAccountDao.delete(account.getId());
entityManager.getTransaction().commit();
Account res = jpaAccountDao.findById(1);
assertEquals(res, account);
assertNull(result);
}
}
(Init.script)
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','[email protected]','90045870', '1980-11-05'); INSERT INTO CUSTOMER (FK_CUSTOMER, firstName, lastName, email, phoneNumber, birth) VALUES (2, 'Silje', 'Kyrra','[email protected]','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 );
Upvotes: 0
Views: 175
Reputation: 42
I just figure it out. It was an error with my testfile =)
I needed to change the method to get a instance, and delete with the method rather than through entityManager =)
Upvotes: 0