为什么Hibernate不能在实体中找到辅助表?

时间:2011-06-17 06:41:08

标签: hibernate join entity left-join

Two related database tables with a left join


我发现这个问题没有答案,许多其他论坛也提到了这个问题。虽然我在数据库中存在表IAS_FORM_BKT_BAYI和IAS_FORM_BKT_BAYI_SO,但带有带注释的实体类(异常下面的源代码)我在JSF&中得到了这个异常。基于Hibernate的应用程序:

org.hibernate.AnnotationException: Cannot find the expected secondary table: no IAS_FORM_BKT_BAYI available for net.ozar.bpm.entity.bkt.Bayi
    org.hibernate.cfg.Ejb3Column.getJoin(Ejb3Column.java:293)
    org.hibernate.cfg.Ejb3Column.getTable(Ejb3Column.java:272)
    org.hibernate.cfg.annotations.SimpleValueBinder.make(SimpleValueBinder.java:222)
    org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1898)
    org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1279)
    org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)
    org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
    org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
    org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1148)
    org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1226)
    org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:173)
    org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854)
    org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:191)
    org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:253)
    org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125)
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
    net.ozar.bpm.util.EntityUtil.getEntityManagerFactory(EntityUtil.java:13)
    net.ozar.bpm.util.EntityUtil.getEntityManager(EntityUtil.java:21)
    net.ozar.bpm.servisler.CalisanJpaController.getEntityManager(CalisanJpaController.java:31)
    net.ozar.bpm.servisler.CalisanJpaController.getPersonId(CalisanJpaController.java:120)
    net.ozar.bpm.web.jsfmanaged.LoginBean.oturumAc(LoginBean.java:122)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.apache.el.parser.AstValue.invoke(AstValue.java:191)
    org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
    com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:387)
    org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:321)
    org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:296)
    org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:253)
    org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:466)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
    org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
    org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
    org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
    org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
    net.ozar.bpm.web.filters.RestrictPageFilter.doFilter(RestrictPageFilter.java:180)

Bayi.java

@Entity
@Table(name = "IAS_FORM_BKT_BAYI")
@SecondaryTable(name="IAS_FORM_BKT_BAYI_SO", pkJoinColumns = @PrimaryKeyJoinColumn(name="CUSTOMER_ID"))
public class Bayi implements Serializable {
private static final long serialVersionUID = 8681843504687728382L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(table="IAS_FORM_BKT_BAYI", name = "CUSTOMER_ID", nullable = false)
    private Integer customerId;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 30)
    @Column(table="IAS_FORM_BKT_BAYI", name = "CUSTOMER_NUMBER", nullable = false, length = 30)
    private String customerNumber;
    @Size(max = 50)
    @Column(table="IAS_FORM_BKT_BAYI", name = "CUSTOMER_NAME", length = 50)
    private String customerName;
    @Size(max = 50)
    @Column(table="IAS_FORM_BKT_BAYI", name = "VERGI_DAIRESI", length = 50)
    private String vergiDairesi;
    @Size(max = 20)
    @Column(table="IAS_FORM_BKT_BAYI", name = "VERGI_NO", length = 20)
    private String vergiNo;
    @Size(max = 150)
    @Column(table="IAS_FORM_BKT_BAYI", name = "OPERATOR", length = 150)
    private String operator;
    @Column(table="IAS_FORM_BKT_BAYI_SO", name = "SOZLESME", length = 5)
    private String sozlesme;
    @Column(table="IAS_FORM_BKT_BAYI_SO", name = "SOZLESME_UYGUN", length = 5)
    private String sozlesmeUygun;
    @Column(table="IAS_FORM_BKT_BAYI_SO", name = "KEFIL_IMZA", length = 5)
    private String kefilImza;
    @Column(table="IAS_FORM_BKT_BAYI_SO", name = "ACIKLAMA", length = 2000)
    private String aciklama;

    public Bayi() {
    }

    // getters and setters
    // ...

}

我真正想要实现的是获取我获得的结果集,运行以下查询映射到Bayi(实体)对象:

SELECT B.CUSTOMER_ID, B.CUSTOMER_NUMBER, B.CUSTOMER_NAME, B.VERGI_DAIRESI, 
B.VERGI_NO, C.SOZLESME, C.SOZLESME_UYGUN, C.KEFIL_IMZA, C.ACIKLAMA  FROM   
IAS_FORM_BKT_BAYI B LEFT JOIN IAS_FORM_BKT_BAYI_SO C ON B.CUSTOMER_ID = 
C.CUSTOMER_ID WHERE ORG_ID = 102

3 个答案:

答案 0 :(得分:6)

当我读错错误消息时,Hibernate尝试访问IAS_FORM_BKT_BAYI作为辅助表。但IAS_FORM_BKT_BAYI是您的主要表,而非次要表。

之前快速查看一些示例(我之前从未使用过@SecondaryTable),我认为Hibernate只希望table属性中的@Column名称仅用于映射到辅助表的属性。 (这与错误信息匹配)

所以我猜你应该从映射到主表的属性的table="IAS_FORM_BKT_BAYI"属性中删除@Column

答案 1 :(得分:1)

*“在快速查看一些示例(我之前从未使用过@SecondaryTable)之前,我认为Hibernate希望@Column属性中的表名仅用于映射到辅助表的属性。(这匹配错误信息) 所以我想你应该从映射到主表的属性的@Column属性中删除table =“IAS_FORM_BKT_BAYI”。“*

这是非常正确的,在我的情况下也是如此。如果该字段属于辅助表,则Hibernate(3.0)将仅查找表属性。 对于属于主表的字段,不需要表属性。 我删除了那个“找不到辅助表”错误。 从框架的角度来看,这很奇怪!它可能被预测为注释的抑制警告......

谢谢你, PRATIK

答案 2 :(得分:0)

它没有在你的代码中看到任何问题亲爱的,但可能是它似乎是一个休眠问题,尝试用小写字母制作你的表名和子表名,那么它会正常工作

从大写到小写的转换对我有用,我希望它也适合你。

@Entity
@Table(name = "ias_form_bkt_bayi")
@SecondaryTable(name="ias_form_bkt_bayi_so", pkJoinColumns = @PrimaryKeyJoinColumn(name="customer_id"))