使用@OneToOne删除实体

时间:2016-11-16 07:10:55

标签: jpa entity sql-delete one-to-one

我有删除实体的问题。 entitymanager不会删除该实体。有没有人在代码中看到错误?

错误讯息:

java.lang.AssertionError: 预期:null 实际:帐户{id = 1,customer = Customer {customerId = 1,firstName ='Kim',lastName ='Pedersen',email ='kim @ yahoo.no',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,title,price,description,number,instantiationDate)VALUES(1,'Mio min Mio',100.0,'Book about two brothers','8-321389213','2016-05-11二十三时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');

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'); < / p>

INSERT INTO LOGIN(FK_LOGIN,用户名,密码)VALUES(1,'kimPedda','kimSimDimSum'); INSERT INTO LOGIN(FK_LOGIN,用户名,密码)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);

1 个答案:

答案 0 :(得分:0)

我只是搞清楚了。我的testfile =)

是一个错误

我需要更改方法以获取实例,并使用方法而不是通过entityManager删除=)

  • 案例已解决