[Hibernate-JIRA] Created: (ANN-548) Joins break when using composite primary key classes
by Vincent Jenks (JIRA)
Joins break when using composite primary key classes
----------------------------------------------------
Key: ANN-548
URL: http://opensource.atlassian.com/projects/hibernate/browse/ANN-548
Project: Hibernate Annotations
Type: Bug
Versions: 3.2.1, 3.2.2
Environment: JBoss 4.0.5.GA w/ Hibernate 3.2.2 and Annoations 3.2.2 (from SVN). Also reproduced w/ JBoss 4.0.5.GA w/ stock annotations, hibernate, and entity manager, unmodified. The attached test-case was run against MSSQL 2000 but is trivial to point at another database. The issue was also reproduced on a OpenEdge Progress 10.1A database.
Reporter: Vincent Jenks
Attachments: CarBean.java, Carz.java, Lotz.java
Joins break when using composite primary key classes
JBoss 4.0.5.GA w/ Hibernate 3.2.2 and Annoations 3.2.2 (from SVN). Also reproduced w/ JBoss 4.0.5.GA w/ stock annotations, hibernate, and entity manager, unmodified. The attached test-case was run against MSSQL 2000 but is trivial to point at another database. The issue was also reproduced on a OpenEdge Progress 10.1A database.
In a case where there are composite keys on one (or each) side of a relationship, Hibernate cannot join the entities together if all of the primary key fields are not used in the relationship.
A (very) simple example, two entities Carz and Lotz (pardon the names, it's simply cars and car lots):
@Entity
@Table(name="car")
public class Carz implements Serializable
{
@Id
private Integer id;
@Column(name="make", nullable=false)
private String make;
@Column(name="model", nullable=false)
private String model;
@Column(name="manufactured", nullable=false)
@Temporal(TemporalType.TIMESTAMP)
private Date manufactured;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumns //Hibernate docs state that @JoinColumns must be used since Lotz has composite PK, a single @JoinColumn does not deploy anyhow
({
@JoinColumn(name="loc_code", referencedColumnName="loc_code", insertable=false, updatable=false)
})
private Lotz lot;
...........................
}
@Entity
@Table(name="lot")
public class Lotz implements Serializable
{
@EmbeddedId
protected LotzPK lotPK;
@Column(name="name", nullable=false)
private String name;
@Column(name="location", nullable=false)
private String location;
@OneToMany(mappedBy="lot", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
private List<Carz> cars;
...........................
}
@Embeddable
public class LotzPK implements Serializable
{
@Column(name="id", nullable=false)
private Integer id;
@Column(name="loc_code", nullable=false)
private String locCode;
...........................
}
In this case I need a one-to-many relationship on Carz.id = Lotz.locCode. However, there doesn't appear to be a way to do this.
When this is deployed, this exception occurs:
org.hibernate.AnnotationException: Column name id of hqb.model.Lotz not found in JoinColumns.referencedColumnName
at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:306)
at org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:64)
at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:433)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:287)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1211)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:847)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:385)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:126)
at org.jboss.ejb3.entity.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:264)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.jboss.ejb3.ServiceDelegateWrapper.startService(ServiceDelegateWrapper.java:102)
at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:289)
at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:245)
at sun.reflect.GeneratedMethodAccessor263.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
...........................
I can deploy the app if I map both fields in the Lotz PK class like so:
@Entity
@Table(name="car")
public class Carz implements Serializable
{
@Id
private Integer id;
...........................
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumns
({
@JoinColumn(name="loc_code", referencedColumnName="loc_code", insertable=false, updatable=false),
@JoinColumn(name="", referencedColumnName="id", insertable=false, updatable=false) //INCORRECTLY DEFINED RELATIONSHIP!
})
private Lotz lot;
...........................
}
But obviously this is no good, it generates bad SQL at runtime:
/* select
c
from
Carz c
left join
fetch c.lot */ select
carz0_.id as id198_0_,
lotz1_.id as id199_1_,
lotz1_.loc_code as loc2_199_1_,
carz0_.make as make198_0_,
carz0_.model as model198_0_,
carz0_.manufactured as manufact4_198_0_,
carz0_.lot_id as lot5_198_0_,
carz0_.loc_code as loc6_198_0_, --no such column...
lotz1_.name as name199_1_,
lotz1_.location as location199_1_
from
car carz0_
left outer join
lot lotz1_
on carz0_.lot_id=lotz1_.id --oops! Hibernate defaulted a relationship here...
and carz0_.loc_code=lotz1_.loc_code
In reality, this is the SQL I'm trying to achieve, by repairing the above query manually:
select
carz0_.id as id198_0_,
lotz1_.id as id199_1_,
lotz1_.loc_code as loc2_199_1_,
carz0_.make as make198_0_,
carz0_.model as model198_0_,
carz0_.manufactured as manufact4_198_0_,
carz0_.loc_code as loc6_198_0_,
lotz1_.name as name199_1_,
lotz1_.location as location199_1_
from
car carz0_
left outer join
lot lotz1_
on carz0_.loc_code=lotz1_.loc_code
This is consistent w/ the EJB 3.0 spec (and Glassfish+Toplink) but that's only because the spec doesn't define exacltly how to handle this. However, that being said, this seems important enough for Hibernate to be able to manage?
--
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
15 years, 4 months
[Hibernate-JIRA] Created: (HHH-3461) Enhance DialectFactory to support Sybase Adaptive Server Anywhere
by Jason Stern (JIRA)
Enhance DialectFactory to support Sybase Adaptive Server Anywhere
-----------------------------------------------------------------
Key: HHH-3461
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3461
Project: Hibernate3
Issue Type: Improvement
Components: core
Affects Versions: 3.2.6
Environment: Hibernate 3.2.6.GA, Sybase Adaptive Server Anywhere
Reporter: Jason Stern
Priority: Minor
Currently, the org.hibernate.dialect.DialectFactory class has native support for Sybase Adaptive Server Enterprise (ASE), but throws the following exception when used with Sybase Adaptive Server Anywhere (ASA):
"org.hibernate.HibernateException: Hibernate Dialect must be explicitly set for database: Adaptive Server Anywhere"
I have manually set the dialect for now, but if you could add the following line of code to the DialectFactory class, it would help with future portability:
MAPPERS.put( "Adaptive Server Anywhere", new VersionInsensitiveMapper( "org.hibernate.dialect.SybaseAnywhereDialect" ) );
Thanks for all your hard work!
--
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
15 years, 4 months
[Hibernate-JIRA] Created: (HHH-3731) ConstraintViolationException logged at level ERROR instead of WARN or INFO
by Jonathan Amir (JIRA)
ConstraintViolationException logged at level ERROR instead of WARN or INFO
--------------------------------------------------------------------------
Key: HHH-3731
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3731
Project: Hibernate Core
Issue Type: Bug
Components: core
Affects Versions: 3.2.6
Environment: hibernate core jar specifies 3.2.6 in META-INF folder. database is oracle 10.2. OS is Win XP
Reporter: Jonathan Amir
Priority: Minor
Attachments: stacktrace.txt
Please see this thread for an example and an explanation of the problem:
http://forums.hibernate.org/viewtopic.php?p=2258025&sid=cdcf98f3e9026fad9...
Basically, it is a valid use-case for a developer to insert a record to the db and to try and catch a ConstraintViolationException. Upon catching it, it should be the developer's choice regarding what to do, how to recover, etc., and also how to log it.
However, Hibernate logs the error before throwing it, and at log level ERROR, no less, thus polluting the log files with superfluous messages. It is not practical to log messages at this Level. The offending class is AbstractFlushingEventListener.java (I'm attaching a stacktrace to illustrate).
Please reduce the log level of this message to WARN or INFO.
There's also a sort of philosophical argument for this request. Some may argue, like in the reply in the linked thread above, that a constraint violation is an error, thus it should be logged as one. However, that is an application-domain error, not a hibernate error. Hibernate is a library, and as such it should conform to user's requirements. First, it should use the ERROR level only to log real errors related to Hibernate's functionality as a library. Secondly, it should aim to give the user maximum flexiblity and control with regards to logging.
(Not withstanding the previous statement, logging application problems is, of course, very helpful for debugging, but it should be at a lower log level)
--
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
15 years, 4 months
[Hibernate-JIRA] Created: (HHH-3891) one-to-one with property-ref always non-lazy
by Sandeep Vaid (JIRA)
one-to-one with property-ref always non-lazy
--------------------------------------------
Key: HHH-3891
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3891
Project: Hibernate Core
Issue Type: Bug
Components: core
Reporter: Sandeep Vaid
I have one-to-one relationship betnwee Product and ProductBasic.
<one-to-one name="productBasic" cascade="save-update" property-ref="activeProduct" lazy="proxy">
<formula>'1'</formula>
<formula>PRODUCTID</formula>
</one-to-one>
ProductBasic.hbm.xml as :
<properties name="activeProduct">
<property name="Code" column="CODE"></property>
<property name="pId" column="PID" insert="false" update="false"></property>
</properties>
When i am using property-ref, and try to load Product, it eagerly fetches ProductBasic also as :
select *
from PRODUCT productbo0_ left outer join PRODUCTPERIOD productbas1_ on '18'=productbas1_.CODE and productbo0_.PID=productbas1_.PID where productbo0_.PID=?
NOTE: Along with Product, ProductBasic is also fetched in a single query.
Even if i add constrained="true" in one-to-one mapping, the query gets fired as :
select * from PRODUCT productbo0_ where productbo0_.PID=?
select * from PRODUCTBASIC productbas0_ where productbas0_.CODE=? and productbas0_.PID=?
NOTE: Along with Product, ProductBasic is also fetched in a different query.
BUT in both these cases, ProductBasic is fetched always (non-lazy)..
How can i fetch ProductBasic in a Non-Lazy manner in this case.
--
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
15 years, 4 months