作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
只有当我将 Tab.java 更改为 @OneToOne(cascade = CascadeType.ALL) 时,此代码才有效。我不明白为什么,在测试中我创建一个独立的标签实例并在选项卡中设置。我认为这应该适用于 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)
类(class)
@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();
}
}
类(class)
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());
}
}
}
最佳答案
如果您想使用 CascadeType.PERSIST
,请使用 persist()
而不是 save()
或者使用CascadeType.SAVE_UPDATE如果您想使用 save(tab)
CascadeType.PERSIST
对于save()
操作,对应的CascadeType是SAVE_UPDATE
,对于persist()
操作,对应的CascadeType是PERSIST
关于java - hibernate4简单的级联持久不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25432698/
我是一名优秀的程序员,十分优秀!