-->
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.  [ 1 post ] 
Author Message
 Post subject: 2 object on 1 table
PostPosted: Thu Nov 08, 2012 4:05 pm 
Newbie

Joined: Thu Nov 08, 2012 3:41 pm
Posts: 1
Hi All!

I have 1 table of objects.
First: in search mode - need to show only name and text property,
Then: in browse/edit mode - need to show name, text, type, datecreate and others.

Here're java entities:

Base object:
Code:
@Entity
abstract class Key {   
   @Id
   @Column(name = "id")
   UUID    id;

   public UUID getId() {
      return id;
   }

   public void setId(UUID id) {
      this.id = id;
   }
}

Link object for search mode:
Code:
@Entity
public class Link extends Key {
   @Column(name = "name")
   private String   name;
   
   @Lob
   @Column(name = "text")
   private String   text;

   public String getName() {
      return name;
   }

   public String getText() {
      return text;
   }   
}

Full object for browse/eit mode:
Code:
@Entity
public class Document extends Link{   
   @Column(name = "type")
   private String   type;

   public String getType() {
      return type;
   }
.....
}



And 2 differnet SQL queries for each object:
1. SELECT d.id, d.name, (CASE WHEN LENGTH(d.text) > 170 THEN SUBSTRING(d.text, 1, 170, CODEUNITS16)||'<b>...</b>' ELSE d.text END) text FROM DLib.dl_documents d
2. SELECT d.id, d.name, d.text, d.type, d.datecreate FROM DLib.dl_documents d


Such way hibernate can't extends class Document from Doclink (f.e. during executing SQL №1 - throws and error of uknown field(s))
Quote:
db.createSQLQuery(sql).addEntity(Link.class);


But if there's another base class(abstract) with all properties of class Doclink, and classes Doclink and Document extends from it - all works.
Code:
@Entity
abstract class Link extends Key {
   @Column(name = "name")
   private String   name;
   
   @Lob
   @Column(name = "text")
   private String   text;

   public String getName() {
      return name;
   }

   public String getText() {
      return text;
   }   
}

public class Doclink extends Link{} -- this class has no different properties from abstract parent
public class Document extends Link{}



Is it possible to map 2 classes (one extends from another) from single table with hibernate? Maybe there's example with Discriminator columns..

Dev settings:

JDK 1.7
Hibernate 4.1.7
DataBase: DB2 9.7

Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.