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

我在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?怎么了

回答:

首先-它不能生成它们,因为您没有告诉它怎么做。使用@GeneratedValue并选择所需的策略。

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

以上是 此类的ID必须在调用save()之前手动分配 的全部内容, 来源链接: utcz.com/qa/400757.html

回到顶部