Why cannot Hibernate find secondary table in an Entity?
I found no answers to this question which is also asked on many other forums. Although I have the tables IAS_FORM_BKT_BAYI and IAS_FORM_BKT_BAYI_SO present in the database, with the annotated entity class (the source code below the exception) I'm getting this exception in my JSF & Hibernate-based application:
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.bu开发者_StackOverflow中文版ildMappings(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
// ...
}
What I'm really trying to achieve is get the resultset which I obtain running the following query mapped to the Bayi (entity) object:
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
When I read the error message correct, then Hibernate tryes to access IAS_FORM_BKT_BAYI as an secondary table. But IAS_FORM_BKT_BAYI is your primary and not secondary table.
After an quick look at some examples (I have never used @SecondaryTable) before, I seams that Hibernate expect the table
name in the @Column
attribute ONLY for the attributes mapped to the secondary table. (This match the error message)
So I guess you should remove the table="IAS_FORM_BKT_BAYI"
from the @Column
attribute of the attributes mapped to the primary table.
*"After an quick look at some examples (I have never used @SecondaryTable) before, I seams that Hibernate expect the table name in the @Column attribute ONLY for the attributes mapped to the secondary table. (This match the error message) So I guess you should remove the table="IAS_FORM_BKT_BAYI" from the @Column attribute of the attributes mapped to the primary table."*
This is quite correct and same in my case. Hibernate (3.0) will only look-up to table attribute if the field belongs to secondary table. For fields, which belongs to primary table, there is no table attribute required. I removed that and got away with "cannot find secondary table" error. Quite weird from the framework perspective ! It may be projected as supressed warnings to the annotations...
Thank you, Pratik
its not seeing any issue in you code dear, but may be it seems a hibernate issue, try to make your table names and subtable name using small letter, then it will work fine
converting from upper to lower case worked for me, i hope it will work for you too.
@Entity
@Table(name = "ias_form_bkt_bayi")
@SecondaryTable(name="ias_form_bkt_bayi_so", pkJoinColumns = @PrimaryKeyJoinColumn(name="customer_id"))
I had a similar problem (something has to do with uppercase table names) and the solution was to set PhysicalNamingStrategyStandardImpl
in the application.properties
:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
精彩评论