-->
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.  [ 2 posts ] 
Author Message
 Post subject: Using Interfaces and <subclass> for implementations
PostPosted: Fri Mar 26, 2004 11:20 am 
Newbie

Joined: Fri Mar 26, 2004 10:59 am
Posts: 2
Location: Germany
Hello,

I am currently switching from OJB to Hibernate due to restrictions that OJB has on working with Lists (OJB does not have out-of-the-box feature of storing the position number of a child)...

I think we all agree that working with interfaces instead of concrete implementations on the business layer is state-of-the-art, right ?

So, in order to persist an object I have a PersistenceManager that is doing most of the work and using Hibernate internally. This was very useful during the transistion phase from OJB, because I only had to modify very few classes. So, for example to obtain all objects of a particalur interface the PersistanceManager is called via code like this:

Code:
persistenceMgr.findAllInstances(Class interfaceClass);


So, I really would like to have a mapping file that looks like this:


Code:
<class name="Interface" type="Iterface">
....
<discriminator column="CLASSNAME" type="string"/>
<subclass name="Impl"/>
</class>


Now, usually there's only one concrete implementation of an Interface, but in order to work I'd need to have a column called "CLASSNAME" in every table and the data of this column is redundant (if we only have one impl.).

Is there anyway to have an implicit mechanism for something like this?

Furthermore, all implenations are inheritated from a basic object (BO), that provides the getter method for the unique ID. There should be no
public method to set the ID, because we don't want anybody to mess with this. The ID should be set directly using reflcetion by the framework.

So, I thought I just use

Code:
<id name="id" column="ID" type="string" unsaved-value="null" access="field">


Now, I get an exception like this:
Code:
net.sf.hibernate.PropertyNotFoundException: field not found: id


which probably occurs, because the specified Interface does not contain this field (obviously). I understand that a PropertyAccessor is requried to handle such a scenario, correct? Or is there any other way?

Any help is appreciated !!!

Regards,

Matio

---------------------------------------------------------------------------
Hibernate version: 2.1.2
Mapping document: not needed (general question)
JavaCode: not needed (general question)
StackTrace: full trace not needed
DB: HSLQDB 1.7.1
Log: not needed [/code]


Top
 Profile  
 
 Post subject: Similar problems
PostPosted: Fri Mar 26, 2004 4:26 pm 
Newbie

Joined: Mon Oct 27, 2003 8:15 pm
Posts: 10
Location: Birmingham, AL
I have a similar design and am having similar issues. I have implemented a class-per-table strategy, but there's quite a bit of collective information (ID generation, Lifecycle code) that's common to all the implementing classes.

Code:
public interface DBEntity extends Lifecycle {
  public abstract Integer getId();
  private abstract void setId(Integer id);
  /** blah blah bla */
}

public abstract class DBEntityImpl implements DBEntity {
  private Integer id;
  public Integer getId() {
    return this.id;
  }
  private void setId(Integer id) {
    this.id = id;
  }
   /** Do Lifecycle implementations here... */
}

public interface Something extends DBEntity {
  public String getData();
  public void setData(String data);
  public Date getDate();
  public void setDate(Date date);
}

public class SomethingImpl extends DBEntityImpl {
  private String data;
  private Date date;
  public String getData() {
    return this.data;
  }
  public void setData(String data) {
    this.data = data
  }
  public Date getDate() {
    return this.date;
  }
  public void setDate(Date date) {
    this.date = date;
  }
}



Is it actually possible to specificy the implementing class of a Hibernate-d interface using subclass without having a discriminator column?

I also have a problem with XDoclet/hibernatedoclet not producing classes that have <id> properties defined in a merge file instead of the class file. Known and ignored problem on the XDoclet JIRA.
http://opensource.atlassian.com/projects/xdoclet/browse/XDT-389

Code:
    /**

    * @hibernate.id generator-class="native" type="Integer" column="ID" unsaved-value="null" length="10"
    * @return Integer

     */

    public Integer getId() {

        return this.id;

    }


I can cut and paste the accessor for the id in each of 75 implementing classes, but that seems a bit much and violates my burning need to move common data as high in the object chain as possible.


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