gpt4 book ai didi

java - hibernate4简单的级联持久不起作用

转载 作者:行者123 更新时间:2023-11-30 03:46:48 24 4
gpt4 key购买 nike

只有当我将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com