2016-11-16 1 views
0

Hi j'ai un problème avec la suppression d'une entité. Le gestionnaire d'entités ne supprimerait pas l'entité. Quelqu'un voit-il l'erreur dans le code?Supprimer une entité avec @OneToOne

Message d'erreur:

java.lang.AssertionError: attendu: null actuelle: compte {id = 1, client = client {customerId = 1, prenom = 'Kim', lastName = 'Pedersen', email = '[email protected]', phoneNumber = '90045870', naissance = 1980-11-05 00: 00: 00.0}, login = 'Connexion {Id = 1, nom d'utilisateur =' kimPedda ', mot de passe =' 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 LIVRE (id, titre, prix, description, numéro, instantiationDate) VALUES (1, «Mio min Mio», 100,0, «Livre sur deux frères», «8-321389213», «2016-05-11 23:42:21»); INSERT INTO BOOK (id, titre, prix, description, nombre, instantiationDate) VALEURS (2, 'Franks dagbok', 10.0, 'Sur la guerre et Auchwitch', '13 -321321321 ',' 2016-11-05 20: 00:00 ');

INSÉRER DANS LE CLIENT (FK_CUSTOMER, prénom, nom, email, numéro de téléphone, naissance) VALUES (1, 'Kim', 'Pedersen', '[email protected]', '90045870', '1980-11-05 '); INSÉRER DANS LE CLIENT (FK_CUSTOMER, prénom, nom, email, numéro de téléphone, naissance) VALUES (2, 'Silje', 'Kyrra', '[email protected]', '45236585', '1999-1-15');

INSÉRER DANS LOGIN (FK_LOGIN, nom d'utilisateur, mot de passe) VALUES (1, 'kimPedda', 'kimSimDimSum'); INSÉRER DANS LOGIN (FK_LOGIN, nom d'utilisateur, mot de passe) VALUES (2, 'Silkyra', 'SanriKorraDigo');

INSÉRER DANS COMPTE (id, FK_CUSTOMER, FK_LOGIN) VALEURS (1, 1, 1); INSÉRER DANS COMPTE (id, FK_CUSTOMER, FK_LOGIN) VALEURS (2, 2, 2);

+1

Postez le code ** ici **, ne pas lier à celui-ci. – QBrute

Répondre

0

Je viens de comprendre. Ce fut une erreur avec mon testfile =)

Je avais besoin de changer la méthode pour obtenir une instance, et supprimer la méthode plutôt que par entityManager =)

  • cas résolu