仅当我将Tab.java更改为@OneToOne(cascade = CascadeType.ALL)时,此代码才有效。我不明白为什么,在测试中我创建了一个独立的Tag实例并在tab中设置。我认为应该与CASCADE TYPE PERSIST一起使用。但是我明白了
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: dominio.Tag
at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:294)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:537)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:311)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:321)
at org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:294)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:4243)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:546)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:232)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:159)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:231)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:102)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:55)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at TabTest.setUpSuite(TabTest.java:28)
班级
@MappedSuperclass
public class BasicEntity {
@Id
@GeneratedValue
private Integer id;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BasicEntity)) return false;
BasicEntity that = (BasicEntity) o;
if (!id.equals(that.id)) return false;
return true;
}
@Override
public int hashCode() {
return id.hashCode();
}
}
班级
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToOne;
@Entity
public class Tab extends BasicEntity{
private String name;
@OneToOne(cascade = CascadeType.PERSIST)
private Tag tag;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Tag getTag() {
return tag;
}
public void setTag(Tag tag) {
this.tag = tag;
}
}
上课
import javax.persistence.Entity;
@Entity
public class Tag extends BasicEntity {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试
import dao.TabRepository;
import dominio.Note;
import dominio.Tab;
import dominio.Tag;
import org.hibernate.Session;
import org.junit.*;
import util.DatabaseConfiguration;
public class TabTest {
private Session session;
@BeforeClass
public static void setUpSuite() throws Exception {
Session someSession = DatabaseConfiguration.getSession();
someSession.getTransaction().begin();
Tab tab = new Tab();
tab.setName("linux");
Tag tag = new Tag();
tag.setName("uma tag");
tab.setTag(tag);
new TabRepository(someSession).save(tab);
someSession.getTransaction().commit();
someSession.close();
}
@AfterClass
public static void clean() throws Exception {
DatabaseConfiguration.closeSessionFactory();
}
@Before
public void setUp() throws Exception {
session = DatabaseConfiguration.getSession();
session.getTransaction().begin();
}
@After
public void tearDown() throws Exception {
session.getTransaction().commit();
session.close();
}
@Test
public void test() throws Exception {
for (Tab tab : new TabRepository(session).findAll()) {
System.out.println("tab title " + tab.getName());
System.out.println("tag name " + tab.getTag().getName());
}
}
}
最佳答案
如果要使用persist()
,请使用save()
代替CascadeType.PERSIST
要么
如果要使用CascadeType.PERSIST
,请使用CascadeType.SAVE_UPDATE代替save(tab)
对于save()
操作,相应的CascadeType为SAVE_UPDATE
,对于persist()
操作,相应的CascadeType为PERSIST