Hibernate保存@Entity父类的@ Non-Entity子类的java对象

时间:2013-12-08 21:01:30

标签: java hibernate

我有两个班级

父:

@Entity
public class Rule{
 private String id;
 private String name;
 private String content;

 // setter and getters here
}

子:

// this is not an entity class (no table exists for this)
public class SignalRule extends Rule{
 private String positionType;
 private String TraderOperation operation;

  // setters and getters here
}

我在做的是:

// instantiate an object of SignalRule
SignalRule signal=new SignalRule();
// set properties

然后我试图通过休眠EntityManager保存对象:

commonDAO.save(signal);

保存方法代码如下:

public void save(Object entity) {

    // TODO: check how to setup transaction support based on annotations
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = null;
    try {
        tx = em.getTransaction();
        tx.begin();
        em.persist(entity);
        tx.commit();
    } catch (RuntimeException e) {
        if (tx != null && tx.isActive())
            tx.rollback();
        logger.error("Can't save object " + entity.getClass() + " "
                + entity + "to DB", e);
        throw e;
    } finally {
        em.close();
    }
}

它说:

错误[CommonDAO]无法保存对象类app.client.shared.bre.model.SignalRule app.client.shared.bre.model.SignalRule@5763ca80to DB [INFO] java.lang.IllegalArgumentException:未知实体:app.client.shared.bre.model.SignalRule [INFO] org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:878) [INFO]在app.server.persistence.CommonDAO.save(CommonDAO.java:69)

我做错了什么?我不能保存子对象。 ?必须创建子类的对象:SignalRule,并将其保存在Rule enity中。是不可能的?能告诉我一个解决方案吗? 谢谢!

0 个答案:

没有答案