Hibernate:当对象包含已保存对象的引用时,无法使用 JPA 保存对象

发布于 2025-01-07 06:33:17 字数 1438 浏览 4 评论 0原文

我有一个雇主表,其架构包含 userprofileId

如下所示的雇主表包含 userprofileid,它是用户表的主键

id                 int(11)      (not NULL)         PRI        
userprofileid      int(11)      (not nUll)                          
organization_name  varchar(50)  (not nUll)  

现在我的要求是为此雇主创建一个 JPA 类。

现在我们有了 Employer.java,其中包含以下条目。

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne(fetch=FetchType.LAZY,cascade = CascadeType.ALL)
    @JoinColumn(name="userProfileId", referencedColumnName="id" )
    @Column( insertable=false ,updatable =false)
    private UserProfile userProfile;

    @Column(name = "organization_name")
    private String organizationName;

现在的障碍是。 我们的 UserProfile 对象是用不同的方法创建的。现在我不想再次向 userprofile 表添加相同的值,因此我在雇主.java 中设置了 insertable = false 和 updatable = false ,以便该字段不会更新;

否,如果我在雇主对象中设置用户配置文件的值,

它会说,

org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.hcentive.core.user.UserProfile; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.hcentive.core.user.UserProfile

但如果我不设置用户配置文件,它会给出以下错误。

Caused by: java.sql.SQLException: Field 'userprofileid' doesn't have a default value

注意我坚持使用:

getJpaTemplate().persist(t);

I have an employer table with schema containing userprofileId

The employer table as stated below is containing userprofileid which is primary key of user table

id                 int(11)      (not NULL)         PRI        
userprofileid      int(11)      (not nUll)                          
organization_name  varchar(50)  (not nUll)  

Now my requirement was create a JPA class for this employer.

Now we have Employer.java, with below entry.

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne(fetch=FetchType.LAZY,cascade = CascadeType.ALL)
    @JoinColumn(name="userProfileId", referencedColumnName="id" )
    @Column( insertable=false ,updatable =false)
    private UserProfile userProfile;

    @Column(name = "organization_name")
    private String organizationName;

Now hurdle is.
Our UserProfile is object is created in different method. Now i dont want to again add same value to userprofile table and so i made insertable = false and updatable = false in employer.java so that the field does not get updated;

No if i set the value of userprofile in employer object

it says

org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.hcentive.core.user.UserProfile; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.hcentive.core.user.UserProfile

but if i dont set the user profile it gives below error.

Caused by: java.sql.SQLException: Field 'userprofileid' doesn't have a default value

Note for persisting I am using :

getJpaTemplate().persist(t);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

≈。彩虹 2025-01-14 06:33:18

您的雇主引用了现有的用户配置文件。如果它已经存在,则它在数据库中。如果它在数据库中,则不能创建新实例,而是从数据库中获取它。要从数据库中获取某些内容,您可以使用 EntityManager.find()EntityManager.getReference() 方法(第二个方法甚至不执行任何 select 语句)。

因此,从注释中删除 insertableupdatable 标志(关联is insertable),并使用类似于以下代码的代码:

UserProfile up = em.getReference(UserProfile.class, userProfileId);
employer.setUserProfile(up);
em.persist(employer);

Your Employer refers to an already existing user profile. If it already exists, it is in the database. If it is in the database, you must not create a new instance, but get it from the database. To get something from the database, you use the EntityManager.find() or EntityManager.getReference() method (the second one doesn't even do any select statement).

So, remove the insertable and updatable flags from your annotation (the association is insertable), and use code similar to the following one:

UserProfile up = em.getReference(UserProfile.class, userProfileId);
employer.setUserProfile(up);
em.persist(employer);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文