简体   繁体   English

休眠实体未持久

[英]Hibernate Entity not being persisted

I am building a web app built on Spring MVC and Hibernate. 我正在构建一个基于Spring MVC和Hibernate的Web应用程序。 I have an existing domain model and the application is largely working. 我有一个现有的域模型,该应用程序正在正常运行。

I have a domain object called Status and a corresponding Spring Service class that contains standard CRUD methods you would expect ( createStatus(Status s) ; etc..). 我有一个名为Status的域对象和一个对应的Spring Service类,其中包含您期望的标准CRUD方法( createStatus(Status s) ;等等。)。 The method to persist a new Status is currently working ok and is as follows: 保持新状态的方法目前可以​​正常使用,如下所示:

@Transactional
    public void createStatus(Status s) {
        statusDao.persist(s);
    }

Everything is happy and working fine. 一切都很顺利,一切都很好。 I then added a new Domain object called Notification and was having problems persisting the object to the DB (nothing was getting saved and no errors were logged) - so to test this, I temporarily tried creating a dummy Notification object and persisting it inside the createStatus() method (I know this is ugly - but the point was to attempt to persist it somewhere I know transactions are working and other objects are persisted - this is to rule out transactional issues). 然后,我添加了一个名为Notification的新Domain对象,并且在将对象持久保存到数据库时遇到了问题(什么都没有保存,也没有记录错误)-为了测试这一点,我暂时尝试创建一个虚拟Notification对象并将其持久保存在createStatus中()方法(我知道这很丑陋-但重点是尝试将其持久化到我知道事务正常且其他对象持久化的某个地方-这是要排除事务性问题)。

The updated create method is as follows: 更新后的创建方法如下:

@Transactional
public void createStatus(Status s) {
    statusDao.persist(s);
    Notification n = new Notification();
    getEntityManager().persist(n);

}

Now when I run it, the Status is still persisted but still the Notification object is not persisted and there are no errors in the log. 现在,当我运行它时,状态仍然保留,但Notification对象仍然没有保留,并且日志中没有错误。

Has anyone experienced similar problems where specific Entities are not being persisted? 有没有人遇到过某些无法持久保留特定实体的类似问题?


UPDATE: 更新:

Below are the Entities (Status and Notification) - it is annotation driven so no hbm files: 以下是实体(状态和通知)-它是注释驱动的,因此没有hbm文件:

Status: 状态:

@Entity
@Table(name = "BF_STATUS")
@DiscriminatorValue("Status")
public class Status extends PersistableObject {

    private static final long serialVersionUID = 4925606208793500282L;


    @Column(name = "TITLE")
    protected String title;

    @Column(name = "DEADLINE")
    private Date deadline;

    @Column(name = "DETAILS")
    @Lob
    protected String details;

...
Accesors/Mutators
}

Notification: 通知:

@Entity
@Table(name = "BF_NOTIFICATION")
@DiscriminatorValue("notification")
public class Notification extends PersistableObject {

    private static final long serialVersionUID = -8935712447903901463L;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "LINKED_ACTIVITY", nullable = true)
    private Activity activity;

    @Column(name = "notificationRead")
    private boolean read = false;

    ...
    Accessors/Mutators

}

I have faced this issue sometime back reasons for this issue: 我有时会遇到此问题,是此问题的原由:

  • Database MySQL - Id was not marked as AutoIncrement , and in my Service impl there was no catch block hence the exception was not logged. 数据库MySQL-Id未标记为AutoIncrement ,并且在我的Service隐含中没有catch块,因此未记录异常。

  • Change in database semantics and again either no catch block or empty catch block with exception not being logged. 数据库语义上的更改,并且再次没有捕获块或为空捕获块,但未记录异常。

  • Incorrect log4j.xml which didn't log the exception at all!, and the exp.printStackTrace() presented itself with the exception. 错误的log4j.xml根本没有记录该异常!并且exp.printStackTrace()呈现了异常。

Now with this can you surround your code with valid try catch and see if there is something ? 现在,您可以用有效的try catch包围代码,看看是否有东西吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM