我在使用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?这有什么不对?
答案 0 :(得分:10)
首先 - 它不能生成它们,因为你没有告诉它如何做到这一点。使用@GeneratedValue
并选择所需的策略。
然后 - 你不能有两个@Id
字段。如果您想要复合ID,请使用@EmbeddedId
或@IdClass
答案 1 :(得分:3)
您在一个表中使用两个主键,其中一个键也是另一个表中的主键。 简单的解决方案是删除一个也是外键的主键
答案 2 :(得分:1)
我遇到了同样的问题,但对我有用的是
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;