@OneToMany
映射的问题。它就像这里 @Entity
@Table(name =albums)
@SequenceGenerator( NAME = aLBUMS_SEQ,sequenceName = albums_seq,allocationSize = 1)
公共类AlbumDs {
@Id @GeneratedValue(策略= GenerationType.SEQUENCE,发电机= aLBUMS_SEQ)
私人整数ID;
private Integer ownerId;
私人字符串名称;
私有字符串描述;
私人创建日期;
@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(创建日期){
this.created = created;
}
public void setUsers(Set< UserAlbumDs> users){
this.users = users;
}
public Set< UserAlbumDs> getUsers(){
返回用户;
当我调用 为什么hibernate无法自行分配这些ID?这里有什么不对? UseraAlbumDs $ c
$ p $ @Entity
@Table(name =users_albums)
public class UserAlbumDs {
@Id
private Integer userId;
@Id
私人整数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(我得到了异常:
SEVERE:Servlet.service()for servlet [main] in上下文与路径[/ Kronika]抛出异常[请求处理失败;嵌套异常是org.hibernate.id.IdentifierGenerationException:必须在调用save()之前手动分配此类的ids(eu.patrykdobrowolski.model.hibernate.UserAlbumDs),其根源为
org.hibernate.id.IdentifierGenerationException:在调用save()之前,必须手动分配此类的id:eu.patrykdobrowolski.model.hibernate.UserAlbumDs $ b $ org.hibernate.id.Assigned.generate(Assigned.java:33)$ b $ 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.Deful tSaveOrUpdateEventListener.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)
位于org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
位于org.hibernate.engine.Cascade。 cascadeCollectionElements(Cascade.java:296)
在org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
在org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
在org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
在org.hibernate.engine.Cascade.cascade(Cascade.java:130)
在org.hibernate .event.def.AbstractSaveEventListener.ca (org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener)中的
。 Java的:181),美元,org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121 b $ b)
在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(De faultSaveOrUpdateEventListener.java:70)
在org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
在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)
在org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:1
在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)$ b在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
$ b at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
@GeneratedValue
并选择你想要的策略。
@Id
字段。如果您想要一个复合标识符,请使用 @EmbeddedId
或 @IdClass
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?
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
这篇关于在调用save()之前,必须手动分配这个类的ID,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!