Hibernate trouble getting composite key to work

markbernard

I have a class called WebAsset:

public class WebAsset {
    private Long id;
    private String url;
    private int status;
    //more fields that are not relevent
}

I need to be able to show relationships between WebAsset, so I created a table for the relationship and a composite key class.

public class WebAssetReferencePK {
    private Long sourceAssetId;
    private Long targetAssetId;
}

public class WebAssetReference {
    private WebAssetReferencePK wpk;
    private Long updateTime;
}

We are forced to use an older version of Hibernate so we need to use xml files instead of annotaions. Here is the mapping for the reference class:

<class name="ca.gc.cra.www.crawler.valueobject.WebAssetReference" table="webassetreference">
    <composite-id name="webAssetReferencePK" class="ca.gc.cra.www.crawler.valueobject.WebAssetReferencePK">
        <key-property name="sourceAsset" type="java.lang.Long" column="sourceAssetId" />
        <key-property name="targetAsset" type="java.lang.Long" column="targetAssetId" />
    </composite-id>
    <property name="updateTime" type="java.lang.Long" column="updatetime" not-null="true" />
</class>

In the composite key I get what I expect in the database with 2 ids related to each other. But when I try to query with HQL or Criteria it doesn't work since there is no direct relation between the PK class and WebAsset and I need to be able to do a join between WebAsset and WebAssetReference. If I try to change the composite key types from java.lang.Long to WebAsset then hibernate stores the whole object in the WebAssetReference table instead of just the ids.

An example of what I am trying to do is if I have a sourceAssetId I want to return all the targetAssetIds with the same source, but I don't want the ids themselves I want the WebAsset that is the primary key for each targetAssetId.

I have been searching around for the answer but every example I can find are just simple examples that don't relate.

Update 1: With continued searching I finally found the answer. Instead of key-property I need to use key-many-to-one. I haven't tried a join yet but everything else looks right so this should be the answer.

Update 2: Can't get the query to work with HQL. Here is th SQL of what I am trying to do:

select * from webasset as wa join webassetreference as war on war.targetassetid=wa.webasset_id where war.sourceassetid=?

Here is the HQL that is not working:

FROM WebAsset JOIN WebAssetReference WebAssetReference.WebAssetReferencePK.targetAsset=WebAsset WHERE WebAssetReference.WebAssetReferencePK.sourceAsset = :sourceAsset

I get the following error with HQL: ERROR - line 1:89: unexpected token: .

I'll keep trying but I can't seem to figure out the HQL.

markbernard

I discovered how to do this. In the case I have above it will not work since I have 2 columns joining to the same table. However if I use the same WebAsset class above and instead use this class:

 public class TreeNode implements Comparable<TreeNode>{

    private String nodeUrl;
    private Long id;
    private Boolean folder;
    private transient WebAsset nodeAsset = null; 
}

With this .hbm.xml file:

<class name="ca.gc.cra.www.crawler.valueobject.TreeNode" table="TreeNode">
    <id name="id" type="java.lang.Long" column="treenode_id" >
        <generator class="identity"/> 
    </id>
    <many-to-one name="nodeAsset" class="ca.gc.cra.www.crawler.valueobject.WebAsset" column="nodeAsset_id" lazy="false" not-null="false" cascade="none" unique="true" />
    <property name="folder" type="java.lang.Boolean" column="folder" not-null="true" />
    <property name="nodeUrl" length="512"  type="java.lang.String" column="nodeUrl" not-null="true" />

    <set name="children" table="TreeNode" inverse="false" lazy="true" >
        <key column="parentnode_id"/>
            <one-to-many class="ca.gc.cra.www.crawler.valueobject.TreeNode" />
    </set>
</class>

You can then use this code to retrieve the join:

Session session = HibernateUtil.getSession();
try {
    String hql = "FROM TreeNode tn JOIN tn.nodeAsset WHERE tn.id=5";
    Query query = session.createQuery(hql);
    List result = query.list();
    System.out.println("done");
} catch (HibernateException e) {
    e.printStackTrace();
    throw new Exception("Query failed", e);
} finally {
    session.flush();
    session.close();
}

Hibernate can then perform the join correctly. The result will be a List containing an Object array for each entry. The Object contains the 2 classes that are part of the join. You have to cast the Object with (Object[]) to access the elements and then cast each on to the appropriate class.

I would recommend against this approach because Hibernate will attempt to load all connected classes as well. With the example above I was getting 1 row from TreeNode yet it generated 19 select statements. I even attempted to set the connected classes to lazy load and it still generated all the selects.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Hibernate trouble getting composite key to work

From Dev

Hibernate mapping for composite key

From Dev

Hibernate @GeneratedValue in a composite key

From Dev

Hibernate Composite Primary Key

From Dev

Hibernate @GeneratedValue in a composite key

From Dev

Hibernate Unidirectional OneToMany with composite key

From Dev

composite key using @idclass in hibernate

From Dev

Hibernate Unidirectional OneToMany with composite key

From Dev

Trouble getting menu to work

From Dev

Hibernate foreign key with a part of composite primary key

From Dev

Composite key including foreign key in hibernate set

From Dev

trouble getting incron inotify to work

From Dev

Trouble getting POST to work with swift

From Dev

Trouble on getting my for loop to work

From Dev

Trouble Getting Database Connection to Work

From Dev

Trouble with getting an AJAX form to work

From Dev

Trouble with getting the jQuery to work in Wordpress

From Dev

Trouble getting regex to work with find

From Dev

Trouble getting ElectronPlayer to work on Lubuntu

From Dev

How to Implement composite primary key with Hibernate Annonations

From Dev

Hibernate inverse join for composite primary key

From Dev

Hibernate Mapped Composite Key ManyToMany results in ClassCastException

From Dev

hibernate update query composite key example

From Dev

Hibernate Mapped Composite Key ManyToMany results in ClassCastException

From Dev

Hibernate inverse join for composite primary key

From Dev

One to many association in Hibernate without composite key

From Dev

Hibernate Composite key Annotation and Database Change

From Dev

Problems with Relationship Composite Primary Key on Hibernate

From Dev

Trouble getting Mandrill Handlebars comparison expression to work

Related Related

  1. 1

    Hibernate trouble getting composite key to work

  2. 2

    Hibernate mapping for composite key

  3. 3

    Hibernate @GeneratedValue in a composite key

  4. 4

    Hibernate Composite Primary Key

  5. 5

    Hibernate @GeneratedValue in a composite key

  6. 6

    Hibernate Unidirectional OneToMany with composite key

  7. 7

    composite key using @idclass in hibernate

  8. 8

    Hibernate Unidirectional OneToMany with composite key

  9. 9

    Trouble getting menu to work

  10. 10

    Hibernate foreign key with a part of composite primary key

  11. 11

    Composite key including foreign key in hibernate set

  12. 12

    trouble getting incron inotify to work

  13. 13

    Trouble getting POST to work with swift

  14. 14

    Trouble on getting my for loop to work

  15. 15

    Trouble Getting Database Connection to Work

  16. 16

    Trouble with getting an AJAX form to work

  17. 17

    Trouble with getting the jQuery to work in Wordpress

  18. 18

    Trouble getting regex to work with find

  19. 19

    Trouble getting ElectronPlayer to work on Lubuntu

  20. 20

    How to Implement composite primary key with Hibernate Annonations

  21. 21

    Hibernate inverse join for composite primary key

  22. 22

    Hibernate Mapped Composite Key ManyToMany results in ClassCastException

  23. 23

    hibernate update query composite key example

  24. 24

    Hibernate Mapped Composite Key ManyToMany results in ClassCastException

  25. 25

    Hibernate inverse join for composite primary key

  26. 26

    One to many association in Hibernate without composite key

  27. 27

    Hibernate Composite key Annotation and Database Change

  28. 28

    Problems with Relationship Composite Primary Key on Hibernate

  29. 29

    Trouble getting Mandrill Handlebars comparison expression to work

HotTag

Archive