-->
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.  [ 3 posts ] 
Author Message
 Post subject: Load an object from DB by superclass type
PostPosted: Mon Jul 10, 2006 12:52 am 
Newbie

Joined: Sun Jul 09, 2006 10:26 pm
Posts: 3
Hi all,

I'm moving project to Hibernate Annotations. We have an abstract class - Lead. It has few subclasses newLead, usedLead etc. (see the code below)


Question:

When I try to get an object from DB
Code:
session.load(Lead.class, leadId);
the "org.hibernate.MappingException: Unknown entity: com.model.Lead" is thrown. It works with
Code:
session.load(NewLead.class, leadId);
, but is it possible to get an object from DB by superclass type? They all stored in the same table and have unique ids anyway. What I did wrong? It worked with classic Hibernate.

Any help will help :)
Thanks in advance.


Hibernate version:
Hibernate 3.2.0CR2
Hibernate Annotations 3.2.0CR1

Mapping documents:
Hibernate Annotations mapping
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="au.com.carsales.sys.lead.model.UsedLead"/>
<mapping class="au.com.carsales.sys.lead.model.NewLead"/>
</session-factory>
</hibernate-configuration>

Classic hibernate mapping used before:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name = "com.model.Lead" table = "LEADS" lazy = "true">
<id name="leadId"
column="MASTER_LEAD_ID"
type = "long"
unsaved-value="0">
<generator class="sequence">
<param name="sequence">
master_lead_id_seq
</param>
</generator>
</id>

<discriminator column="LEAD_TYPE_ID" type="string" />

<component name="leadStatus"
class="au.com.carsales.sys.lead.model.LeadStatus">

<property name="statusId"
column="LEAD_STATUS_ID"
type="long" />

<property name="statusDesc"
column="LEAD_STATUS_DESCRIPTION"
type="string" />
</component>

....

<subclass name="com.model.NewLead"
discriminator-value="1">
</subclass>

<subclass name="com.model.UsedLead"
discriminator-value="2">

</subclass>
</class>

</hibernate-mapping>

Code
Code:
@MappedSuperclass
@Table(name = "LEADS")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "LEAD_TYPE_ID", discriminatorType = DiscriminatorType.STRING)
@org.hibernate.annotations.Entity(optimisticLock = OptimisticLockType.VERSION)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public abstract class Lead implements Serializable {

    private long leadId;
    private LeadStatus leadStatus;

@Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_GEN")
    @SequenceGenerator(name = "SEQ_GEN", sequenceName = "master_lead_id_seq", allocationSize = 1)
    @Column(name = "MASTER_LEAD_ID")
    public long getLeadId() {
   return leadId;
    }

    public void setLeadId(long leadId) {
   this.leadId = leadId;
    }

    @Embedded
    @AttributeOverrides( {
       @AttributeOverride(name = "statusId", column = @Column(name = "LEAD_STATUS_ID")),
       @AttributeOverride(name = "statusDesc", column = @Column(name = "LEAD_STATUS_DESCRIPTION")) })
    public LeadStatus getLeadStatus() {
   return leadStatus;
    }

    public void setLeadStatus(LeadStatus leadStatus) {
   this.leadStatus = leadStatus;
    }
.....
}

@Entity
@Table(name = "LEADS")
@DiscriminatorValue("2")
public class UsedLead extends Lead implements Serializable {

    private static final long serialVersionUID = -3344961002006417591L;

}


@Entity
@Table(name = "LEADS")
@DiscriminatorValue("1")
public class NewLead extends Lead implements Serializable {

    private static final long serialVersionUID = -4344961002306417591L;

}


@Embeddable
public class LeadStatus implements Serializable {
    private static final long serialVersionUID = 606276831663986341L;

    private long statusId;

    private String statusDesc;

    public String getStatusDesc() {
   return statusDesc;
    }

    public void setStatusDesc(String statusDesc) {
   this.statusDesc = statusDesc;
    }

    public long getStatusId() {
   return statusId;
    }

    public void setStatusId(long statusId) {
   this.statusId = statusId;
    }

    public int hashCode() {
   return this.statusDesc.hashCode();
    }

    public boolean equals(Object o) {
   if (o instanceof LeadStatus)
       return ((LeadStatus) o).getStatusDesc()
          .equals(this.getStatusDesc());

   return false;

    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 10, 2006 7:22 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
map lead as @Entity not @MappedSuperclass

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 12:57 am 
Newbie

Joined: Sun Jul 09, 2006 10:26 pm
Posts: 3
Thanks, mate. It helped a lot :)


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