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: HQL and class extension
PostPosted: Thu Oct 28, 2010 1:43 am 
Newbie

Joined: Thu Oct 14, 2010 3:01 am
Posts: 3
Hi, I have an entity class, class B, that extends from a base entity class.
The base entity class has two fields.
There is a parent class, class A, which has a one-to-many relation with class B.

My problem is that when i try to retrieve class B from class A, the fields in the base entity class is not reflecting in the HQL.

The code for the base entity is as given below:
Code:
public abstract class BaseEntity {
private String name;
private String value;
protected BaseEntity() {}
@Column(name = "NAME")
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   @Column(name = "VALUE")
   public String getValue() {
      return value;
   }
   public void setValue(String value) {
      this.value= value;
   }
   public NameVO retrieveNameVO() {
      NameVO nameVO = new NameVO();
      String name = this.getName();
      if (null != name && name.length() > 0) {
         nameVO.setName(name);
         nameVO.setValue(this.getValue());
      }
      return nameVO;
   }
}


The class which extends base entity is:
Code:
@Entity
@Table(name = "MST_B")
public class B extends BaseEntity {
   
   protected B () {
super();
   }
   
   private BPK bPK;

   @EmbeddedId
   @AttributeOverrides({
         @AttributeOverride(name = "code", column = @Column(name = "CODE")),
         @AttributeOverride(name = "number", column = @Column(name = "NUMBER")),
         @AttributeOverride(name = "id", column = @Column(name = "NAME_ID"))})
   public BPK getBPK() {
      return bPK;
   }

   public void setBPK(BPK bPK) {
      this.bPK = bPK;
   }
}


And finally the parent class
Code:
@Entity
@Table(name = "MST_A")
public class A {

   private String route;
   private Set<B> bSet;

   /*
    * default constructor for JPA
    */
   protected A() {
   }

   public A(AVO aVO) {
   }

   public AVO retrieveAVO() {
      AVO vo = new AVO();
      vo.setRoute(this.getRoute());
      vo.setNameVOs(retrieveNameVOs());
      return vo;
   }
   
   private List<NameVO> retrieveNameVOs() {
      List<NameVO> nameVOs = new LinkedList<NameVO>();
      for (B b : bSet) {
         NameVO nameVO = b.retrieveNameVO();
         nameVOs.add(nameVO);
      }
      return nameVOs;
   }

   @Column(name = "ROUTE")
   public String getRoute() {
      return route;
   }

   public void setRoute(String route) {
      this.route = route;
   }
   
   @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   @JoinColumns({
         @JoinColumn(name = "CODE", referencedColumnName = "CODE", insertable = false, updatable = false),
         @JoinColumn(name = "NUMBER", referencedColumnName = "NUMBER", insertable = false, updatable = false) })
   public Set<B> getbSet() {
      return bSet;
   }

   public void setbSet(Set<B> bSet) {
      this.bSet = bSet;
   }
}


And the HQL query is
Quote:
StringBuilder sqlQuery = new StringBuilder()
.append("select distinct parentClass from A parentClass")
.append(" left join fetch parentClass.bSet child");


The sql generated takes the child class into consideration, but the column NAME and VALUE doesnt get reflected in the query.
Please help me.


Top
 Profile  
 
 Post subject: Re: HQL and class extension
PostPosted: Thu Oct 28, 2010 2:09 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You need to add @MappedSuperclass annotation to your superclass:

Code:
@MappedSuperclass
public abstract class BaseEntity {...


Top
 Profile  
 
 Post subject: Re: HQL and class extension
PostPosted: Thu Oct 28, 2010 3:05 am 
Newbie

Joined: Thu Oct 14, 2010 3:01 am
Posts: 3
Hi nordborg,

Thank you for the response. I tried that, and it has worked.


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.