• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate mapping quiz

 
tylor james
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I designing an app, and I have something like the code bellow, I can't find a way to map it. What I want to do I to map it all in 1 table, and I don't know if it's posible, as car 'has a' Value this should be mapped as a component, but component doesn't support inheritance, I could map it as Object, but, How to map the Interfaces, in order to make polimorphic queries to retrieve for sell/rent cars or both. I'm a bit desperated. Thanks all in advance.

public class Car
{
//with getters and setters
private Value value;
...
}


public abstract class Value
{
}

public interface Let
{
public double getRent();
public setRent(double rent);
}

public interface Sell
{
public double getPrice();
public setPrice(double price);
}

public class ValueSellImpl extends Value implements Sell
{ ... }

public class ValueLetImpl extends Value implements Let
{ ... }

public class ValueLetSellImpl extends Value implements Let, Sell
{ ... }
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I can see from your code, you only have one object which might need persisting (and therefore mapping) and that is Car. How do you map the interfaces? Well you don't, since they represent funtionallity not data.
[ October 27, 2004: Message edited by: Paul Sturrock ]
 
tylor james
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I know that the only class I have to map is Car, but how should I map Value in order to make queries using polymorphism through the interfaces or directly with the classes that implement them.
Thank you anyway.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

how should I map Value in order to make queries using polymorphism through the interfaces

Why would you wnat to? - your Value objects adjust one attribute (a double) depending on which piece of business functionality you are using (Let, Sell or LetSell). You should be persisting the result of these caluclations, not the calculations themselves.
[ October 27, 2004: Message edited by: Paul Sturrock ]
 
tylor james
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, my piece of code is no very clear, the class ValueLetSellImpl has two doubles: rent and price, I think should map these values in some way. I thikn I'll have an instance of car with a Value instance, that could be an instance one of the three subclasses of Value, and I guess hibernate will be able to store the values of the Value instance properly, am I right int this?
Thanks again
 
tylor james
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I miss to say that as ValueLetSellImpl has price and rent, ValueLetImpl have rent and ValueSellImpl has price. I'm sorry about that
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about what you are asking from an ER point of view - you want to map an attribute of Car which may map to one column, or another, or both. That's an unecessarily convoluted mapping.

Consider what you have. You have the attributes of a car, but depending on the car's state there may be extra attributes (price and/or rent). Now you could lazily model this a one table, with price and rent data added as required. If you did that, then all you need is a Car POJO. But for business reasons you may want to store things as effectively Car, SoldCar, LetCar and LetSoldCar. In which case you'd have four entities, of which Car defines the "base" properties. In which case its your Car POJO which would need mapped (with three subclasses) not your Value.
 
tylor james
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, but if I do that my model gets too complicated, imagine that I have things like limousine, utilitary, your design make this grow exponentially, but you didn't know it :-). I'm trying to merge the state and the data of the object all in the object value. I think your design make sense only for a limited model, imagine that, using your proposal, I say that if want to create the class Carshop that 'has a' car or a collection of, and I should create a subclass of carshop called letcarshop and other soldcarshop.
thanks again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic