2016-11-16 2 views
0

こんにちは、エンティティの削除に問題があります。エンティティマネージャはエンティティを削除しません。コード内にエラーが表示されますか?@OneToOneでエンティティを削除する

エラーメッセージ:

にjava.lang.AssertionError: 予想:ヌル 実際:アカウント{ID = 1、顧客=顧客{はcustomerId = 1、firstNameという= 'キム'、lastNameの= 'ペダーセン' email = '[email protected]'、phoneNumber = '90045870'、birth = 1980-11-05 00:00:00.0}、login = 'ログイン{ID = 1、ユーザー名=' kimPedda '、パスワード=' 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、 '2人の兄弟についての書籍'、 '8-321389213'、'2016-05-11 23:42:21 '); INSERT INTO BOOK(ID、タイトル、価格、説明、番号、インスタンシエーション日時)VALUES(2、 'Franks dagbok'、10.0、 '戦争とAuchwitchについて'、'13 -321321321 '、'2016-11-05 20: 00:00 ');

VALUES(1、 'キム'、 'ペデルセン'、 '[email protected]'、 '90045870'、 '1980-11-05'、 'ファーストネーム、ファーストネーム、ファーストネーム、ファーストネーム、 '); (2、 'Silje'、 'Kyrra'、 '[email protected]'、 '45236585'、 '1999-1-15')VALUES(FK_CUSTOMER、ファーストネーム、ラストネーム、Eメール、電話番号、生年月日)

VALUES(1、 'kimPedda'、 'kimSimDimSum'); VALUES(2、 'Silkyra'、 'SanriKorraDigo');ユーザのログイン名を入力してください(FK_LOGIN、ユーザ名、パスワード)。

(ID、FK_CUSTOMER、FK_LOGIN)VALUES(1、1、1); INSERT INTO ACCOUNT アカウントに挿入(ID、FK_CUSTOMER、FK_LOGIN)VALUES(2、2、2);

+1

ポスト解決しましたコード**ここ**、それにリンクしないでください。 – QBrute

答えて

0

わかりました。それは私がインスタンスを取得する方法を変更し、この方法ではなく、

  • ケース)のEntityManager =を通じて削除するのに必要な

    )=私のテストファイルでエラーだった

関連する問題