-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Embedding single table hierarchy
PostPosted: Fri Jul 25, 2008 2:45 pm 
Newbie

Joined: Fri Jul 25, 2008 2:22 pm
Posts: 3
Hi all,

We are trying to embed a single table strategy as one column in a table that represents another entity. In other words, we don't want a one-to-one relationship (with 2 tables), but instead we want the column to be part of the same table.

Here's an example:

public class Client{
private Long id;
private int field1;
private int field2;

private Strategy myStrategy;

public void doSomething(){
myStrategy.doSomething(field1);
}
}

public abstract class Strategy{
public abstract void doSomething(int num);
}

public class StrategyA{
public void doSomething(int num){
...
}
}

public class StrategyB{
public void doSomething(int num){
...
}
}


table Client:

id field1 field2 Strategy
----------------------------------
2 6 5 StrategyA
4 2 3 StrategyB
7 5 9 StrategyB
9 7 7 StrategyA


Does anybody know how to achieve this with annotations?
Which classes should be mapped in the XML?
Which one is an entity and which one is not?
Is there any MappedSuperClass?
What about Embeddable?
Is the inheritance mapping SINGLE_TABLE?

Hibernate version:
3.2

Mapping documents:
We wish we knew...

Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 27, 2008 11:20 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
To simplify this, is what you're asking to have two classes to map to one database table with Hibernate? You're Client and Strategy to one single table?

I've got a little Hibernate on this topic, you should really check it out.

Mapping Two Classes to One DB Table with Hibernate3 and JPA Annotations

The key is the @Embeddable annotation. You embed one class into the other, namely the strategy into the client.


Image


Code:
package com.examscam.mappings;
import javax.persistence.Embeddable;

@Embeddable
public class ThingDetail {

   private String alias;
   private int count;

   public String getAlias() {return alias;}
   public void setAlias(String alias) {
      this.alias = alias;
   }

   public int getCount() {return count;}
   public void setCount(int count) {
      this.count = count;
   }
}


Code:
package com.examscam.mappings;
import javax.persistence.*;
import org.hibernate.Session;
import com.examscam.HibernateUtil;

@Entity
public class Thing {
  private long id; 
  private String name;

  private ThingDetail thingDetail;

@Embedded
  public ThingDetail getThingDetail(){
    return thingDetail;
  }
  public void setThingDetail(ThingDetail detail) {
    this.thingDetail = detail;
  }

  @Id
  @GeneratedValue
  public long getId() {return id;}
  public void setId(long id) {this.id = id;}

  public String getName() {return name;}
  public void setName(String name) {this.name = name;}
}


As I said, check out the full tutorial:

Mapping Two Classes to One DB Table with Hibernate3 and JPA Annotations

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 28, 2008 2:05 pm 
Newbie

Joined: Fri Jul 25, 2008 2:22 pm
Posts: 3
Thanks Cameron for the reply, but you overlooked the problem.

I know how to embed classes, but the problem here is to do it with a hierarchy.
If you check the example I gave, you will see that Strategy is an abstract class that has some subclasses, and what I want to store on the client is what subclass the client is using.. Strategies are stateless, so they don't have any persistent value except for its own name to identify it.

I would be able to do the persistence in 2 tables, by storing the Strategy with InheritanceType.SINGLE_TABLE and a DiscriminatorColumn and a OneToOne relationship with the Client. But that way I would loose performance with JOINS that I don't need because I would be storing one concept that should be in one table in 2 tables just because I don't know how to solve it with Hibernate.

Does someone know if there's a way to do what I described? Can someone solve the example I described in one table? Is there a way to hack hibernate to achieve this maybe with ANSI SQL?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2008 3:05 am 
Newbie

Joined: Fri Mar 14, 2008 4:20 am
Posts: 17
Did you find a solution?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.