-->
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: How can I associate with a "plain" interface?
PostPosted: Thu Dec 08, 2005 2:21 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 10:20 am
Posts: 32
Location: Brazil
I'm using Hibernate 3.0.5. I want to map the following classes:
Code:
interface State {
  public String getName();
  public State[] getNextStates();
}

class BaseState implements State {
  private Long id;
  private String name;
  private State[] nextStates;
 
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public State[] getNextStates() {
    return nextStates;
  }
  public void setNextStates(State[] nextStates) {
    this.nextStates = nextStates;
  }
  public Long getId() {
    return id;
  }
  public void setId(Long id) {
    this.id = id;
  }
}

class Item {
  private Long id;
  private String name;
  private State state;
 
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Long getId() {
    return id;
  }
  public void setId(Long id) {
    this.id = id;
  }
  public State getState() {
    return state;
  }
  public void setState(State states) {
    this.states = state;
  }
}
with the following mapping:
Code:
<hibernate-mapping>
  <class table="state" name="BaseState">
    <id column="state_id" name="id">
      <generator class="native"/>
    </id>
    <property name="name" not-null="true" length="60" type="string" column="name"/>
  </class>
  <class table="item" name="Item">
    <id column="item_id" name="id">
      <generator class="native"/>
    </id>
    <property name="name" not-null="true" length="60" type="string" column="name"/>
    <many-to-one column="state_id" name="state" class="State"/>
  </class>
</hibernate-mapping>

But I get an error:
Quote:
An association from the table items refers to an unmapped class: State

So I added:
Code:
<class table="state" name="State">
</class>

But now the State's class mapping won't validate because the class element needs an id element.

My question is: How can I make my Item class have a State(the interface, not the implementation) member, without adding getId() to State's interface?

Thanks in advance,
Daniel Serodio


Top
 Profile  
 
 Post subject: Same problem! Please answer!
PostPosted: Wed Dec 28, 2005 12:51 pm 
Newbie

Joined: Wed Dec 28, 2005 10:21 am
Posts: 8
I have exactly the same problem in some points of my application.
Did you find a solution?
Or could anybody please help both of us?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 6:18 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
<many-to-one column="state_id" name="state" class="BaseState"/>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 7:29 pm 
Newbie

Joined: Tue Oct 18, 2005 2:49 pm
Posts: 13
I agree with baliukas, you really must have State abstract and BaseState as concrete so you can implement either table per hirearchy or table per subclass to get any polymorphism out of it.
An interface, as it cannot have properties, should not belong in your data model, IMHO.

Essentially you would have a mapping for the abstract State class and the have a <subclass> or <joined-subclass> element in it for BaseState.


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.