2016-11-16 31 views
0

您好,我有刪除實體的問題。實體管理者不會刪除實體。有沒有人看到代碼中的錯誤?用@OneToOne刪除實體

錯誤消息:

java.lang.AssertionError: 預計:空 實際:帳戶{ID = 1,客戶=客戶{客戶ID = 1,名字= '金',姓氏= '佩德森', 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(); 
} 

}

公共類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,標題,價格,描述,數量,instantiationDate)VALUES( 1,'Mio min Mio',100.0,'關於兩兄弟的書','8-321389213','2016-05-11 23:42:21'); INSERT INTO BOOK(id,title,price,description,number,instantiationDate)VALUES(2,'Franks dagbok',10.0,'關於戰爭和Auchwitch','13 -321321321','2016-11-05 20: 00:00'); (1,'Kim','Pedersen','[email protected]','90045870','1980-11-05(0)評論(0)隱藏評論提問時間2007-06-01 20:10:10我要評論「); INSERT INTO CUSTOMER(FK_CUSTOMER,firstName,lastName,email,phoneNumber,birth)VALUES(2,'Silje','Kyrra','[email protected]','45236585','1999-1-15'); (FK_LOGIN,用戶名,密碼)VALUES(1,'kimPedda','kimSimDimSum'); INSERT INTO LOGIN(FK_LOGIN,username,password)VALUES(2,'Silkyra','SanriKorraDigo'); (ID,FK_CUSTOMER,FK_LOGIN)VALUES(1,1,1);

INSERT INTO ACCOUNT(id,FK_CUSTOMER,FK_LOGIN)VALUES(1,1,1); INSERT INTO ACCOUNT(id,FK_CUSTOMER,FK_LOGIN)VALUES(2,2,2);

+1

郵政代碼**在這裏**,請不要鏈接到它。 – QBrute

回答

0

我只是想通了。這是我的testfile的錯誤=)

我需要改變的方法來獲取一個實例,並與方法,而不是通過EntityManager的=)

  • 案例刪除解決