hibernate4简单级联持久不起作用

时间:2014-08-21 17:46:06

标签: java hibernate cascade

此代码仅在我将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;
}

}

ouhter class

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());
    }
}

}

1 个答案:

答案 0 :(得分:2)

如果您想使用persist()

,请使用save()代替CascadeType.PERSIST

或 如果您想使用CascadeType.PERSIST

,请使用CascadeType.SAVE_UPDATE代替save(tab)

对于save()操作,相应的CascadeType为SAVE_UPDATE,对于persist()操作,相应的CascadeType为PERSIST