在调用 save() 之前必须手动分配此类的 ids

发布于 2024-10-26 20:11:39 字数 6501 浏览 5 评论 0原文

我在 hibernate @OneToMany 映射方面遇到了一些问题。它就像这样

@Entity
@Table(name = "albums")
@SequenceGenerator(name = "ALBUMS_SEQ", sequenceName = "albums_seq", allocationSize = 1)
public class AlbumDs {

    @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ALBUMS_SEQ")
    private Integer id;
    private Integer ownerId;
    private String name;
    private String description;
    private Date created;
    @OneToMany(cascade = CascadeType.ALL) 
    @JoinColumn(name = "albumId", insertable = true, updatable = true)
    private Set<UserAlbumDs> users = new HashSet<UserAlbumDs>();

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Integer getOwnerId() {
        return ownerId;
    }
    public void setOwnerId(Integer ownerId) {
        this.ownerId = ownerId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public Date getCreated() {
        return created;
    }
    public void setCreated(Date created) {
        this.created = created;
    }
    public void setUsers(Set<UserAlbumDs> users) {
        this.users = users;
    }
    public Set<UserAlbumDs> getUsers() {
        return users;
    }
}

UseraAlbumDs

@Entity
@Table(name = "users_albums")
public class UserAlbumDs {

    @Id
    private Integer userId;
    @Id
    private Integer albumId;

    public Integer getUserId() {
        return userId;
    }
    public void setUserId(Integer userId) {
        this.userId = userId;
    }
    public Integer getAlbumId() {
        return albumId;
    }
    public void setAlbumId(Integer albumId) {
        this.albumId = albumId;
    }
}

当我调用 save(albumDs) 时,我得到异常:

SEVERE: Servlet.service() for servlet [main] in context with path [/Kronika] threw exception [Request processing failed; nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): eu.patrykdobrowolski.model.hibernate.UserAlbumDs] with root cause
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): eu.patrykdobrowolski.model.hibernate.UserAlbumDs
    at org.hibernate.id.Assigned.generate(Assigned.java:33)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:99)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
    at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
    at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
    at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
    at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
    at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
    at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
    at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:334)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
    at eu.patrykdobrowolski.dao.hibernate.HibernateAlbumDao.create(HibernateAlbumDao.java:26)
    at eu.patrykdobrowolski.service.impl.AlbumServiceImpl.createAlbum(AlbumServiceImpl.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)

为什么 hibernate 不能自行分配这些 id?这是怎么回事?

I've got some problem with hibernate @OneToMany mapping. It goes like here

@Entity
@Table(name = "albums")
@SequenceGenerator(name = "ALBUMS_SEQ", sequenceName = "albums_seq", allocationSize = 1)
public class AlbumDs {

    @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ALBUMS_SEQ")
    private Integer id;
    private Integer ownerId;
    private String name;
    private String description;
    private Date created;
    @OneToMany(cascade = CascadeType.ALL) 
    @JoinColumn(name = "albumId", insertable = true, updatable = true)
    private Set<UserAlbumDs> users = new HashSet<UserAlbumDs>();

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Integer getOwnerId() {
        return ownerId;
    }
    public void setOwnerId(Integer ownerId) {
        this.ownerId = ownerId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public Date getCreated() {
        return created;
    }
    public void setCreated(Date created) {
        this.created = created;
    }
    public void setUsers(Set<UserAlbumDs> users) {
        this.users = users;
    }
    public Set<UserAlbumDs> getUsers() {
        return users;
    }
}

UseraAlbumDs:

@Entity
@Table(name = "users_albums")
public class UserAlbumDs {

    @Id
    private Integer userId;
    @Id
    private Integer albumId;

    public Integer getUserId() {
        return userId;
    }
    public void setUserId(Integer userId) {
        this.userId = userId;
    }
    public Integer getAlbumId() {
        return albumId;
    }
    public void setAlbumId(Integer albumId) {
        this.albumId = albumId;
    }
}

When I call save(albumDs) I get exception:

SEVERE: Servlet.service() for servlet [main] in context with path [/Kronika] threw exception [Request processing failed; nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): eu.patrykdobrowolski.model.hibernate.UserAlbumDs] with root cause
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): eu.patrykdobrowolski.model.hibernate.UserAlbumDs
    at org.hibernate.id.Assigned.generate(Assigned.java:33)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:99)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
    at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
    at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
    at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
    at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
    at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
    at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
    at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:334)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
    at eu.patrykdobrowolski.dao.hibernate.HibernateAlbumDao.create(HibernateAlbumDao.java:26)
    at eu.patrykdobrowolski.service.impl.AlbumServiceImpl.createAlbum(AlbumServiceImpl.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)

Why hibernate can't assing those ids on its own? What's wrong here?

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

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

发布评论

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

评论(3

林空鹿饮溪 2024-11-02 20:11:39

首先 - 它无法生成它们,因为你没有告诉它如何去做。使用@GenerateValue并选择您想要的策略。

那么 - 你不能有两个 @Id 字段。如果您想要复合 ID,请使用 @EmbeddedId@IdClass

First - it can't generate them because you didn't tell it how to do it. Use @GeneratedValue and choose the strategy you want.

Then - you can't have two @Id fields. If you want a composite id use @EmbeddedId or @IdClass

迷鸟归林 2024-11-02 20:11:39

您在一个表中使用两个主键,其中一个键也是另一个表中的主键。
简单的解决方案是删除主键之一(也是外键)

You are using two primary keys in one table and one of the keys is also a primary key in another table.
The simple solution is to remove the one of the primary keys which is also the foreign key

灰色世界里的红玫瑰 2024-11-02 20:11:39

我面临着同样的问题,但对我有用的

B oB = new B();        // Class B contains two objects of class A

A oA1 = new A();
oA1.Name = "First";
oB.PlaceA1 = oA1;

A oA2 = new A();
oA2.Name = "Second";
oB.PlaceA2 = oA2;

B oB = new B();        // Class B contains two objects of class A

A oA1 = new A();
oA1.ID = 1;            // This was missing(Manual Assignment of ID field)
oA1.Name = "First";
oB.PlaceA1 = oA1;

A oA2 = new A();
oA2.ID = 2;            // This was missing(Manual Assignment of ID field)
oA2.Name = "Second";
oB.PlaceA2 = oA2;

I was facing the same problem, but what worked for me was

B oB = new B();        // Class B contains two objects of class A

A oA1 = new A();
oA1.Name = "First";
oB.PlaceA1 = oA1;

A oA2 = new A();
oA2.Name = "Second";
oB.PlaceA2 = oA2;

Changed to

B oB = new B();        // Class B contains two objects of class A

A oA1 = new A();
oA1.ID = 1;            // This was missing(Manual Assignment of ID field)
oA1.Name = "First";
oB.PlaceA1 = oA1;

A oA2 = new A();
oA2.ID = 2;            // This was missing(Manual Assignment of ID field)
oA2.Name = "Second";
oB.PlaceA2 = oA2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文