当前位置: 首页 > 知识库问答 >
问题:

用@OneToOne删除实体

吴驰
2023-03-14

你好,我在删除实体时遇到了问题。entitymanager不会删除实体。有人看到代码中的错误吗?

错误消息:

java.lang.Assertionerror: 预期 :null 实际 :帐户{id=1, 客户=客户 id=1, 名字=“金”, 姓氏=“佩德森”, email='kim@yahoo.no“, 电话号码=”90045870“, 出生=1980-11-05 00:00:00.0}, 登录名= ”登录名{Id=1, 用户名='金佩达', 密码='金吉姆吉姆''}}

@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();
}

}

公共类Account tServiceIT{

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);
}

}

(初始化脚本)

在书中插入(id、标题、价格、描述、编号、实例化日期)值(1,'Mio min Mio',100.0,'BOOK about two brothers','8-321389213','2016-05-11 23:42:21');在书中插入(id、标题、价格、描述、编号、实例化日期)值(2,“Franks dagbok”,10.0,“关于战争和Auchwitch”,“13-321321321”,“2016-11-05 20:00:00”);

在客户(FK_CUSTOMER,firstName,lastName,email,phoneNumber,birth)中插入值(1,'Kim','Pedersen','kim@yahoo.no','90045870', '1980-11-05'); 在客户(FK_CUSTOMER,firstName,lastName,email,phoneNumber,birth)中插入值(2,'Silje','Kyrra','silje@yahoo.no','45236585', '1999-1-15');

在登录(FK_LOGIN、用户名和密码)中插入值(1,'kimpeda','kimSimDimSum');在登录(FK_LOGIN、用户名和密码)中插入值(2,'Silkyra','SanriKorraDigo');

插入到帐户(id,FK_CUSTOMER,FK_LOGIN)VALUES(1,1,1);插入到帐户(id,FK_CUSTOMER,FK_LOGIN)VALUES(2,2,2);

共有1个答案

邵星光
2023-03-14

我只是想办法。这是我的testfile=)的错误

我需要更改方法来获得一个实例,并使用该方法而不是通过entityManager =)进行删除

  • 案件已解决
 类似资料:
  • 我有两个具有一对一关系的实体,A和B。B实体是可选的,可以自行更新和删除,但必须始终链接到A的实例。 所以我有两个JPA实体,A和B具有双向关系。这是从A到B的那个。 我可以创建a和B,删除a,然后两者都被删除。好的 但由于从A到B的级联,如果i<code>em。remove(b)删除不会持久化。即使我做了。 在保留级联的同时删除可选实体的唯一方法似乎是使用新的JPA2特性orphanRemova

  • 问题内容: 有没有人解决此问题:https : //hibernate.atlassian.net/browse/HHH-9663? 我也面临着类似的问题。当我在两个实体之间创建单面(无反向引用)一对一关系并将孤立删除属性设置为true时,将引用设置为null后,引用的对象仍在数据库中。 这是示例域模型: 我目前正在通过手动删除孤儿来解决此问题。 问题答案: 级联仅对从 父级 传播到 子级的 实体

  • 删除父实体时,我还想删除关联的子实体(从数据库中)。我试图在删除时使用级联,如下所示,但我一定做错了什么。 当对父实体对象调用删除时,我收到错误消息:“该实体仍在数据库的其他地方引用”。我可以确认该实体在数据库的其他地方引用的唯一地方是在下面的两个表中(如果我手动从数据库中删除子行,对父实体对象的删除调用工作正常)。在过去的9个小时里,我一直在阅读实体对象并尝试不同的东西。我做错了什么? 这是我的

  • 主要内容:JPA实体删除示例要从数据库中删除记录,可以使用接口提供方法。方法使用主键来删除特定的记录。 JPA实体删除示例 在这里,我们将演示如何根据主键删除指定学生的信息。 完整的项目代码如下所示 - 这个例子包含以下步骤 - 第1步: 在包下创建一个名为的实体类,它包含属性:,和。 文件:StudentEntity.java 的代码如下 - 第2步: 将实体类和其他数据库配置映射到文件中。 文件:persistence.

  • 代码: 三个问题: > 我必须放置<code>CascadeType吗。在两个实体中删除?我希望它能正常工作,这样如果我删除了老师,课程就会自动删除。 在我的数据库中,我的id无法正常工作。我希望它使id每次增加一个,但它目前增加了一些随机数。为什么呢? 我还有两个类,但有关系。当我想在数据库中持久化一个新对象时,我必须做什么?

  • 我的实体。ValidationStep与documentDetail有一对一的关系,documentDetail与documentValidations有一个完全的关系 我的删除查询 父ValidationStep被删除,但是docDetail和documentValidations仍然在数据库中。