Hibernate:当对象包含已保存对象的引用时,无法使用 JPA 保存对象
我有一个雇主表,其架构包含 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的雇主引用了现有的用户配置文件。如果它已经存在,则它在数据库中。如果它在数据库中,则不能创建新实例,而是从数据库中获取它。要从数据库中获取某些内容,您可以使用
EntityManager.find()
或EntityManager.getReference()
方法(第二个方法甚至不执行任何 select 语句)。因此,从注释中删除
insertable
和updatable
标志(关联is insertable),并使用类似于以下代码的代码: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()
orEntityManager.getReference()
method (the second one doesn't even do any select statement).So, remove the
insertable
andupdatable
flags from your annotation (the association is insertable), and use code similar to the following one: