-->
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.  [ 8 posts ] 
Author Message
 Post subject: Mapping problens: associations with interfaces
PostPosted: Tue Aug 05, 2008 11:11 pm 
Newbie

Joined: Fri Aug 01, 2008 9:13 am
Posts: 4
Hibernate version: 3.2.6.ga

Hi,


I'm using Annotations for mapping.

I was trying to mapping the following structure:

Code:
public class Order
{
  // geters/seters

  private IDestination destination;
}

public interface IDestination
{
  Long getId();
  void setId(Long id);
}

public class Destination1 implements IDestination
{

  // implementation

  private Long id;
}

public class Destination2 implements IDestination
{

  // implementation

  private Long id;
}


I tried to do the mapping like this:

Code:
@Entity
public class Order
{
  // geters/seters

  @ManyToOne
  @JoinTable
  private IDestination destination;
}

@MappedSuperclass
public interface IDestination
{
  @Id
  Long getId();
  void setId(Long id);

}

@Entity
public class Destination1 implements IDestination
{

  // implementation

  private Long id;
}

@Entity
public class Destination2 implements IDestination
{

  // implementation

  private Long id;
}


When I tried to generate the database schema, it was not possible, and I got this errors messages:

1º.: The id was not defined to Destination1 and Destination2
2º.: IDestination is not a entity

is it possible to implement this kind of structure?

I want to the database schema to be like this.
Tables:

Order
Destination1
Destination2
Order_Destination1
Order_Destination2

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 9:02 pm 
Newbie

Joined: Fri Aug 01, 2008 9:13 am
Posts: 4
I'm really in trouble here..

Somebody, help-me please.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 08, 2008 4:38 pm 
Newbie

Joined: Wed Dec 21, 2005 12:44 pm
Posts: 6
I think that the issue you're running into here is that Hibernate can't infer an inheritance strategy (and thus cannot create the appropriate relationships in the database) from your mapped superclass.

Essentially, the Order table will need to have a foreign key to the destination table to satisfy the @ManyToOne relationship - but since there's no common base class, Hibernate can't figure out what that foreign key should point to.

Keep in mind that Hibernate is quite a leaky abstraction; you still need to think about your data structure in terms of the database in a large degree. The easy solution in your case would be to make an abstract Destination base class (presuming that you don't have other base classes for your Destination entities) and define the inheritance type there.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 9:46 am 
Newbie

Joined: Fri Aug 01, 2008 9:13 am
Posts: 4
nuttycom wrote:
I think that the issue you're running into here is that Hibernate can't infer an inheritance strategy (and thus cannot create the appropriate relationships in the database) from your mapped superclass.

Essentially, the Order table will need to have a foreign key to the destination table to satisfy the @ManyToOne relationship - but since there's no common base class, Hibernate can't figure out what that foreign key should point to.

Keep in mind that Hibernate is quite a leaky abstraction; you still need to think about your data structure in terms of the database in a large degree. The easy solution in your case would be to make an abstract Destination base class (presuming that you don't have other base classes for your Destination entities) and define the inheritance type there.


But, are you saying to me that, that kind of structure is not possible to do with Hibernate?

IDestination must be an interface, becouse other classes implements and extends other classes.

I was looking at the Hibernate documentation and I have found the Tuplizer concept, but, I didn't understood how to use it, i didn't found any examples of it.

Can someone help me?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 19, 2008 6:06 am 
Newbie

Joined: Tue Sep 16, 2008 11:45 am
Posts: 3
Hi,
I have exactly the same problem. And I can't believe Hibernate cant solve it, but I am stuck...

Is this a design error, because it seems that nobody else has this problem, and I couldnt find any examples. The reason why I want to design my application this way is:

I want a modular structure, assume the following modules:

Fishes
People
Furniture

All these classes are mapped to the DB, so far, so good. Now assume I want to add an additional module, called TAGGING.

I want to include as little tagging specific code in the other modules. So my idea was to add an interface "ITagable" to the other three classes. In my Tag-Instance, i can hold a list of references to ITagable, representing the tagged object. (I could, but I dont want to use a reference to OBJECT, by the way, I think this would cause the same problem)...

So how do I map this in Hibernate. And please, there is NO way to make my classes inherit from a common base class, this would mean my domain model becomes a slave to the ORM. But until now I always got the impression that hibernate is different, so this is normally never neccessary...

Chris


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 19, 2008 8:05 pm 
Newbie

Joined: Fri Aug 01, 2008 9:13 am
Posts: 4
Finally, someone with the same problem :)

And I have the same idea, I don't want to change my domain model because I don't want it to become a slave to the ORM too.

So, I was thinking, if we use an Entity listener to load/persist the correct instance, we wouldn't have to change our domain model.

The idea is:

To map the individual entities and create the structure manually at the data base, and, when its needed, we can use entity listeners to load/persist the entities.


Think about that.


I'm writing the code, but I don't have time to finish that right now.

see u.

(sorry about the terrible English).


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 21, 2008 1:18 pm 
Newbie

Joined: Tue Sep 16, 2008 11:45 am
Posts: 3
Hi,
ok, this look interesting. But it still seems not quite like the best way. If I understand you correctly, you suggest to use an entity listener defined for the interface class to identify the base class...

There are several questions coming up, like: can I use an entity listener to change the object which should be persited and actually persist something else (because this is what we would have to do)...

But is there really nobody able to help us with the original question: "Is it possible"..

Sorry for the repition, but I read the whole "Hibernate in Action" and found nothing. Perhaps I am complety on the wrong page, and interfaces are not so commonly used in Java (I am working with .NET and Nhibernate)...

Is here nobody who can explain to us two newbies at least if it is possible (with reasonable amount of work) to:

Map a List of Interface Objects

Christian

PS: Is there something in Java, like in CSharp, where you can construct lists of interfaces instead of using the real class or OBJECT... I really like this, e.g.

Ball implements Throwable
Stone implements Throwable
Tomato implements Throwable

Stuff_I_Can_Throw = new List<Throwable>()


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 7:44 am 
Newbie

Joined: Tue Sep 16, 2008 11:45 am
Posts: 3
Ok
I finally got some answers on the nhibernate mailing list (after 5 min :-)
The keywords are "any" or "many to any" mapping...

I havent figured it out completely, but there are some docs here:
http://www.hibernate.org/hib_docs/nhibe ... tions.html

and a blog post
http://ayende.com/Blog/archive/2006/06/ ... sCool.aspx

Just as a hint for all the other newbies... "Many to Any" is specific enough to torture google with it..

Chris


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.