[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-440) Extend properties on dynamic-component in sublcass
Davide Baroncelli (JIRA)
noreply at atlassian.com
Mon Oct 2 12:43:24 EDT 2006
[ http://opensource.atlassian.com/projects/hibernate/browse/HHH-440?page=comments#action_24691 ]
Davide Baroncelli commented on HHH-440:
---------------------------------------
May I ask if polymorphic components have been eventually implemented (*properly* or not ;) )? As far as I can see Hibernate 3.x feature set does not mention them, and it's a pity: the forums seem to have had various persons asking this feature. I'm asking this because I'm currently implementing a usertype for having component polymorphism and it's a pain in the ass: it involves a lot of "mapping" work that makes one feel as if there wasn't an ORM framework at all! And the normal hibernate hooks on creation do not work, and a lot of other problems...
> Extend properties on dynamic-component in sublcass
> ---------------------------------------------------
>
> Key: HHH-440
> URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-440
> Project: Hibernate3
> Type: Patch
> Components: core
> Versions: 3.0.2
> Environment: hibernte 3.0.2, hsqldb
> Reporter: William F. DeMoss II
> Assignee: Steve Ebersole
> Attachments: patch.txt
>
> Original Estimate: 1 hour
> Remaining: 1 hour
>
> Allow subclasses to add properties to super classes dynamic-component mapping. The was pretty much already implemented except for correct resolution from alias to column names. The following patch contains the fix as well as a test case. This patch is for hibernate 3.0.2, but it also cleanly applys on HEAD. All test cases that were passing before the patch pass after the patch.
> patch.txt
> Index: src/org/hibernate/persister/entity/BasicEntityPersister.java
> ===================================================================
> RCS file: /cvsroot/hibernate/Hibernate3/src/org/hibernate/persister/entity/BasicEntityPersister.java,v
> retrieving revision 1.27
> diff -u -r1.27 BasicEntityPersister.java
> --- src/org/hibernate/persister/entity/BasicEntityPersister.java 26 Apr 2005 20:28:27 -0000 1.27
> +++ src/org/hibernate/persister/entity/BasicEntityPersister.java 4 May 2005 23:45:55 -0000
> @@ -1107,9 +1107,15 @@
> * which takes the entity name.
> */
> protected int getSubclassPropertyTableNumber(String propertyName) {
> - Type type = propertyMapping.toType(propertyName);
> + final String rootPropertyName = StringHelper.root(propertyName);
> + Type type = propertyMapping.toType(rootPropertyName);
> if ( type.isAssociationType() && ( (AssociationType) type ).useLHSPrimaryKey() ) return 0;
> - int index = ArrayHelper.indexOf( getSubclassPropertyNameClosure(), propertyName); //TODO: optimize this better!
> + if ( type.isComponentType() && !propertyName.equals(rootPropertyName) ) {
> + String unrooted = StringHelper.unroot(propertyName);
> + int idx = ArrayHelper.indexOf( getSubclassColumnClosure(), unrooted );
> + if ( idx != -1 ) return getSubclassColumnTableNumberClosure()[idx];
> + }
> + int index = ArrayHelper.indexOf( getSubclassPropertyNameClosure(), rootPropertyName); //TODO: optimize this better!
> return index==-1 ? 0 : getSubclassPropertyTableNumber(index);
> }
>
> Index: src/org/hibernate/persister/entity/BasicEntityPropertyMapping.java
> ===================================================================
> RCS file: /cvsroot/hibernate/Hibernate3/src/org/hibernate/persister/entity/BasicEntityPropertyMapping.java,v
> retrieving revision 1.1
> diff -u -r1.1 BasicEntityPropertyMapping.java
> --- src/org/hibernate/persister/entity/BasicEntityPropertyMapping.java 13 Feb 2005 11:50:09 -0000 1.1
> +++ src/org/hibernate/persister/entity/BasicEntityPropertyMapping.java 4 May 2005 23:45:55 -0000
> @@ -29,9 +29,8 @@
> }
>
> public String[] toColumns(final String alias, final String propertyName) throws QueryException {
> - final String rootPropertyName = StringHelper.root(propertyName);
> return super.toColumns(
> - persister.generateTableAlias( alias, persister.getSubclassPropertyTableNumber(rootPropertyName) ),
> + persister.generateTableAlias( alias, persister.getSubclassPropertyTableNumber(propertyName) ),
> propertyName
> );
> }
> Index: test/org/hibernate/test/component/A.java
> ===================================================================
> RCS file: test/org/hibernate/test/component/A.java
> diff -N test/org/hibernate/test/component/A.java
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ test/org/hibernate/test/component/A.java 1 Jan 1970 00:00:00 -0000
> @@ -0,0 +1,42 @@
> +package org.hibernate.test.component;
> +
> +import java.util.Map;
> +
> +public class A {
> +
> + public int id;
> + public Map dynComp;
> +
> + public A() {
> + this(-1);
> + }
> +
> + public A(int id) {
> + this.id = id;
> + }
> +
> + public Map getDynComp() {
> + return dynComp;
> + }
> +
> + public void setDynComp(Map dynComp) {
> + this.dynComp = dynComp;
> + }
> +
> + public int getId() {
> + return id;
> + }
> +
> + public void setId(int id) {
> + this.id = id;
> + }
> +
> + public boolean equals(Object obj) {
> + if ( !( obj instanceof A ) ) return false;
> + return id == ( (A) obj ).id;
> + }
> +
> + public int hashCode() {
> + return id == -1 ? super.hashCode() : id;
> + }
> +}
> Index: test/org/hibernate/test/component/ABC.hbm.xml
> ===================================================================
> RCS file: test/org/hibernate/test/component/ABC.hbm.xml
> diff -N test/org/hibernate/test/component/ABC.hbm.xml
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ test/org/hibernate/test/component/ABC.hbm.xml 1 Jan 1970 00:00:00 -0000
> @@ -0,0 +1,30 @@
> +<?xml version="1.0"?>
> +<!DOCTYPE hibernate-mapping PUBLIC
> + "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
> + "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
> +
> +<hibernate-mapping package="org.hibernate.test.component">
> +
> + <class name="A">
> + <id name="id"/>
> + <dynamic-component name="dynComp">
> + <property name="a" type="string"/>
> + </dynamic-component>
> + </class>
> +
> + <joined-subclass name="B" extends="A">
> + <key column="A"/>
> + <dynamic-component name="dynComp">
> + <property name="b" type="string"/>
> + </dynamic-component>
> + </joined-subclass>
> +
> + <joined-subclass name="C" extends="B">
> + <key column="B"/>
> + <dynamic-component name="dynComp">
> + <property name="c" type="string"/>
> + </dynamic-component>
> + </joined-subclass>
> +
> +
> +</hibernate-mapping>
> Index: test/org/hibernate/test/component/B.java
> ===================================================================
> RCS file: test/org/hibernate/test/component/B.java
> diff -N test/org/hibernate/test/component/B.java
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ test/org/hibernate/test/component/B.java 1 Jan 1970 00:00:00 -0000
> @@ -0,0 +1,13 @@
> +package org.hibernate.test.component;
> +
> +
> +public class B extends A {
> +
> + public B() {
> + super(-1);
> + }
> +
> + public B(int id) {
> + super(id);
> + }
> +}
> Index: test/org/hibernate/test/component/C.java
> ===================================================================
> RCS file: test/org/hibernate/test/component/C.java
> diff -N test/org/hibernate/test/component/C.java
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ test/org/hibernate/test/component/C.java 1 Jan 1970 00:00:00 -0000
> @@ -0,0 +1,13 @@
> +package org.hibernate.test.component;
> +
> +
> +public class C extends B {
> +
> + public C() {
> + super(-1);
> + }
> +
> + public C(int id) {
> + super(id);
> + }
> +}
> Index: test/org/hibernate/test/component/DynamicComponentTest.java
> ===================================================================
> RCS file: test/org/hibernate/test/component/DynamicComponentTest.java
> diff -N test/org/hibernate/test/component/DynamicComponentTest.java
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ test/org/hibernate/test/component/DynamicComponentTest.java 1 Jan 1970 00:00:00 -0000
> @@ -0,0 +1,84 @@
> +package org.hibernate.test.component;
> +
> +import java.util.HashMap;
> +import java.util.List;
> +
> +import org.hibernate.classic.Session;
> +import org.hibernate.test.TestCase;
> +
> +public class DynamicComponentTest extends TestCase {
> +
> + public DynamicComponentTest(String x) {
> + super(x);
> + }
> +
> + public void testQuery() throws Exception {
> + Session session = openSession();
> +
> + A a = new A(1);
> + a.setDynComp(new HashMap());
> + a.getDynComp().put("a", "a-a");
> + session.save(a);
> +
> + B b = new B(2);
> + b.setDynComp(new HashMap());
> + b.getDynComp().put("a", "b-a");
> + b.getDynComp().put("b", "b-b");
> + session.save(b);
> +
> + C c = new C(3);
> + c.setDynComp(new HashMap());
> + c.getDynComp().put("a", "c-a");
> + c.getDynComp().put("b", "c-b");
> + c.getDynComp().put("c", "c-c");
> + session.save(c);
> +
> + session.flush();
> +
> + List list = session.createQuery("from A a where a.dynComp.a = ?")
> + .setParameter(0, "a-a")
> + .list();
> +
> + assertEquals(1, list.size());
> + assertTrue(a.equals(list.get(0)));
> +
> + list = session.createQuery("from B b where b.dynComp.b = ?")
> + .setParameter(0, "b-b")
> + .list();
> +
> + assertEquals(1, list.size());
> + assertTrue(b.equals(list.get(0)));
> +
> + list = session.createQuery("from B b where b.dynComp.a = ?")
> + .setParameter(0, "b-a")
> + .list();
> +
> + assertEquals(1, list.size());
> + assertTrue(b.equals(list.get(0)));
> +
> + list = session.createQuery("from C c where c.dynComp.c = ?")
> + .setParameter(0, "c-c")
> + .list();
> +
> + list = session.createQuery("from C c where c.dynComp.b = ?")
> + .setParameter(0, "c-b")
> + .list();
> +
> + list = session.createQuery("from C c where c.dynComp.a = ?")
> + .setParameter(0, "c-a")
> + .list();
> +
> + assertEquals(1, list.size());
> + assertTrue(c.equals(list.get(0)));
> +
> + session.delete(c);
> + session.delete(b);
> + session.delete(a);
> + session.close();
> + }
> +
> + protected String[] getMappings() {
> + return new String[] { "component/ABC.hbm.xml" };
> + }
> +
> +}
--
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.jspa
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira
More information about the hibernate-issues
mailing list