[
http://opensource.atlassian.com/projects/hibernate/browse/ANN-541?page=co...
]
Adam T commented on ANN-541:
----------------------------
Please note, the zip file contains a hibernate.cfg.xml file configured to use annotations
for all classes. To reproduce the bug, un/comment the necessary lines to use the mixed
annotation and xml mappings.
Bug Mixing Annotations with Mapping Files. (Minimal Test Case
Attached)
-----------------------------------------------------------------------
Key: ANN-541
URL:
http://opensource.atlassian.com/projects/hibernate/browse/ANN-541
Project: Hibernate Annotations
Type: Bug
Environment: Hibernate Annotations 3.2.1.GA
Hibernate 3.2.1
Reporter: Adam T
Attachments: HibernateMixedMappingsTestCase.zip
I have found a bug in Hibernate when mixing annotations and mapping files. I have
generated a minimal test case. Additionally, I have confirmed that the error does not
appear when all classes use annotations.
There are two mapped classes AImpl and BImpl. AImpl extends BImpl. A third annotated
class ZImpl, refers to BImpl through a ManyToOne relationship. All method signatures refer
to these classes by their interfaces A, B, and Z; however, Hibernate is instructed to use
the *Impl classes when necessary.
I am NOT mixing annotations and mappings in a single inheritance hierarchy. All classes
implement Serializable by extending their Serializable interfaces.
The minimal test case is attached as a zip and included below:
13:02:57,811 INFO [Version] Hibernate Annotations 3.2.1.GA
13:02:57,827 INFO [Environment] Hibernate 3.2.1
13:02:57,827 INFO [Environment] hibernate.properties not found
13:02:57,842 INFO [Environment] Bytecode provider name : cglib
13:02:57,842 INFO [Environment] using JDK 1.4 java.sql.Timestamp handling
13:02:57,936 INFO [Configuration] configuring from resource: /hibernate.cfg.xml
13:02:57,936 INFO [Configuration] Configuration resource: /hibernate.cfg.xml
13:02:58,077 INFO [Configuration] Reading mappings from resource : B.hbm.xml
13:02:58,342 INFO [Configuration] Reading mappings from resource : A.hbm.xml
13:02:58,389 INFO [Configuration] Configured SessionFactory: null
13:02:58,655 INFO [HbmBinder] Mapping class: test.AImpl -> A
13:02:58,670 INFO [AnnotationBinder] Binding entity from annotated class: test.ZImpl
13:02:58,702 INFO [EntityBinder] Bind entity test.ZImpl on table Z
13:02:58,780 ERROR [HibernateUtil] Building SessionFactory failed.
org.hibernate.AnnotationException: @OneToOne or @ManyToOne on test.ZImpl.b references an
unknown entity: test.BImpl
at org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:56)
at
org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:428)
at
org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:286)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1283)
at org.illumen.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:85)
at test.TestMixedMappings.main(TestMixedMappings.java:10)
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.illumen.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:93)
at test.TestMixedMappings.main(TestMixedMappings.java:10)
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on test.ZImpl.b
references an unknown entity: test.BImpl
at org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:56)
at
org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:428)
at
org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:286)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1283)
at org.illumen.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:85)
... 1 more
hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.datasource">java:/comp/env/jdbc/newsoncongress</property>
<property
name="hibernate.current_session_context_class">thread</property>
<property
name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property
name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<mapping resource="B.hbm.xml" />
<mapping resource="A.hbm.xml" />
<mapping class="test.ZImpl" />
</session-factory>
</hibernate-configuration>
A.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 9, 2006 6:27:53 PM by Hibernate Tools 3.2.0.beta7 -->
<hibernate-mapping>
<class name="test.AImpl" table="A" proxy="test.A">
<id name="aId" column="aID"
type="java.lang.Integer">
<generator class="identity" />
</id>
</class>
</hibernate-mapping>
B.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 9, 2006 6:27:53 PM by Hibernate Tools 3.2.0.beta7 -->
<hibernate-mapping>
<joined-subclass name="test.BImpl" table="B"
proxy="test.B"
extends="test.AImpl">
<key column="aID" />
<property name="bId" column="bID"
type="java.lang.Integer"
not-null="true" />
</joined-subclass>
</hibernate-mapping>
A.java:
package test;
public interface A extends java.io.Serializable {
public Integer getAId();
public void setAId(Integer aId);
}
B.java:
package test;
public interface B extends A {
public Integer getBId();
public void setBId(Integer bId);
}
Z.java:
package test;
public interface Z extends java.io.Serializable {
public Integer getZId();
public void setZId(Integer zId);
public B getB();
public void setB(B b);
}
AImpl.java:
package test;
public class AImpl implements A {
private static final long serialVersionUID = 1L;
private Integer aId = 0;
public AImpl() {
}
public Integer getAId() {
return this.aId;
}
public void setAId(Integer aId) {
this.aId = aId;
}
}
BImpl.java:
package test;
public class BImpl extends AImpl implements B {
private static final long serialVersionUID = 1L;
private Integer bId = 0;
public BImpl() {
super();
}
public Integer getBId() {
return bId;
}
public void setBId(Integer bId) {
this.bId = bId;
}
}
ZImpl.java:
package test;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@org.hibernate.annotations.Proxy(proxyClass = Z.class)
@Table(name = "Z")
public class ZImpl implements Z {
private static final long serialVersionUID = 1L;
private Integer zId = null;
private B b = null;
@Id
@Column(name = "zID")
public Integer getZId() {
return zId;
}
public void setZId(Integer zId) {
this.zId = zId;
}
@ManyToOne(optional = false, targetEntity = BImpl.class, fetch = FetchType.LAZY)
@JoinColumn(name = "bID", referencedColumnName = "bID")
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
}
...
When the AImpl and BImpl classes are annotated, the error disappears.
AImpl.java:
package test;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@org.hibernate.annotations.Proxy(proxyClass = A.class)
@Table(name = "A")
public class AImpl implements A {
private static final long serialVersionUID = 1L;
private Integer aId = 0;
public AImpl() {
}
@Id
@Column(name = "aID")
public Integer getAId() {
return this.aId;
}
public void setAId(Integer aId) {
this.aId = aId;
}
}
BImpl.java:
package test;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@org.hibernate.annotations.Proxy(proxyClass = B.class)
@Table(name = "B")
public class BImpl extends AImpl implements B {
private static final long serialVersionUID = 1L;
private Integer bId = 0;
public BImpl() {
super();
}
public Integer getBId() {
return bId;
}
public void setBId(Integer bId) {
this.bId = bId;
}
}
That's all folks!
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira