【问题标题】:hibernate entity mapping null objecthibernate实体映射空对象
【发布时间】:2014-09-15 12:54:52
【问题描述】:

我有一个休眠映射,

@Entity
@Table(name="user")
public class User{


@ManyToOne(optional=true)
@JoinColumn(name="email_id",nullable=true)
private Email email;

}

在我的映射中,Email 对象有时会为空。出现以下错误,因为 email_id 和 email 字段不为空,我只想在用户表上插入,如果对象为空,则需要跳过电子邮件表。我该如何处理这种情况?

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing

如果我使用 @ManyToOne(cascade=CascadeType.ALL), am getting null exception

请帮助我,提前谢谢

【问题讨论】:

    标签: hibernate jakarta-ee


    【解决方案1】:

    我认为你应该在同一个对象 (user) 中有 email_id,如果你希望 Email 表被只读,那么你的电子邮件就不会被持久化。

    【讨论】:

    • 不行,我需要分离这个Email对象,我不能在用户对象中使用这个,因为有时会插入Email对象。
    • @user1335605,我并不是说你要保留user 中的所有属性。只需将email_id 保留为user 中的属性即可。
    【解决方案2】:

    也许您可以使用 referencedColumnName 选项补充您的 @JoinColumn 注释以指定电子邮件的 PK 列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 2018-12-10
      相关资源
      最近更新 更多