-->
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: Is it possible to use abstract classes as entity classes?
PostPosted: Sun Apr 15, 2007 2:48 am 
Newbie

Joined: Sat Apr 14, 2007 4:24 am
Posts: 2
H all:

I would like to use abstract classes as entities to keep the interface/implementation separation. For example:

@Entity
public abstract class User {
@Id @GeneratedValue
private String id_;

@Column(nullable=false)
private String email_;
public String getId() {
return id_;
}
public void setId(String id) {
id_ = id;
}
public String getEmail() {
return email_;
}
public void setEmail(String email) {
email_ = email;
}
public abstract void doSomeThingUserful();
}

public UserImpl extends User {
public void doSomeThingUserful() {
// Execute some businese logic
}
}

I then add the following to the hibernate.cfg.xml:
<mapping class='User"/>

At runtime, I instantiate UserImpl instead of User. However, when I tried to save the object, I get the following error:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.qme.bo.impl.UserImpl

Can anyone please tell me what is the best way to go about doing this?

Thanks,

Stanley


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 15, 2007 5:38 am 
Beginner
Beginner

Joined: Thu Apr 12, 2007 3:26 am
Posts: 35
Location: Germany
You must annotate the extending class (UserImpl) as entity too, then it should work.:

Code:
@Entity
public class UserImpl extends User {
      ....
}


There exists severall strategies with which you can change how such class hierarchies are mapped to the database (all in one table, in different tables etc.). See the hibernate documentation for futher details.

Hoeft


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 15, 2007 6:01 am 
Newbie

Joined: Sat Apr 14, 2007 4:24 am
Posts: 2
Hoeft:

Thanks for your response:

But the problem is that I don't want to persist UserImpl, as it has no part in the modelling aspect. Think of User and UserImpl as modelling the single "User" object. I just want to separate the "User" object into a java abstract class (User) and a java implementation class (UserImpl). This allows me to plug in different implementation class at runtime, with different implementation of the core business logic methods.

Thanks,

Stanley


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 15, 2007 6:24 am 
Beginner
Beginner

Joined: Thu Apr 12, 2007 3:26 am
Posts: 35
Location: Germany
I don't think that this is possible - but I'm really new to hibernate so I don't have discovered all of the wonders of hibernate ;-). How should Hibernate know which class it should take if a new User is instantiated. But if it is possible I would be very interested in the sollution so it would be very kind of you if you post it here.

I think the Mapping-Class-Hierarchie Single Table would suit to your problem. You won't have any new tables for each implementation but only one extra collumn which is needed by hibernate to choose which implementation it should take. If the extending classes don't have extra attributes, I think this is the best Strategy.

If you don't like it, I think you have to create separate businessObjekts which aren't persistent and remove the abstract keyword from the User.


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.