Domanda

We've been getting a weird exception from Hibernate trying to update a non-existent table. Following is a simple example (Hibernate 3.6.0 Final + Oracle 11g + Pure Java).

Here Class_A is abstract in a TABLE_PER_CLASS hierarchy strategy (table per concrete class). Here's the simple version, please tell me what's wrong with our code:

Class_A

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Class_A {
    @Id
    @Column(name="blabla")
    public long myId = 0;
}

Class_B

@Entity
public class Class_B extends Class_A {
    private String myString = "Hellos - I'm Class_B!";
}

Class_C

@Entity
public class Class_C extends Class_A {
    private String myString = "Hellos - I'm Class_C!";

    @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn
    @OrderColumn(name="list_order")
    public List<Class_A> as;
}

Code

Class_C c = new Class_C();
c.myId = 92;
Class_B b = new Class_B();
b.myId = 10;
List<Class_A> bs = new ArrayList<Class_A>();
bs.add(0, b);
c.as = bs;

Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(c);
session.getTransaction().commit();
session.close();

Crash

At commit(), Hibernate tries this SQL:
Hibernate: update Class_A set as_blabla=?, list_order=? where blabla=?

Then crashes on:

java.sql.BatchUpdateException: ORA-00942: table or view does not exist

    at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:17660)
    at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:771)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:188)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)

Why does Hibernate do this?

Much thanks!
- Ten of a Kind

È stato utile?

Soluzione

I think you need to make the relationships bidirectional, and map the one-to-many as the inverse relationship of the many-to-one. Indeed, if you look at http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/inheritance.html#inheritance-limitations, you'll note that polymorphic one-to-many is limited to inverse="true" only in the table per concrete-class strategy.

Thus, try this :

In class A :

@ManyToOne
@JoinColumn
private Class_C c;

In class C :

@OneToMany(mappedBy = "c")
private List<Class_A> as;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top