-->
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.  [ 9 posts ] 
Author Message
 Post subject: Using Annotations
PostPosted: Thu Feb 12, 2009 5:29 am 
Beginner
Beginner

Joined: Thu Sep 18, 2008 5:18 am
Posts: 28
Hi, am generally new on Hibernate and my task is to re-write a component written in Struts to Using hibernate + Spring. I have an fair background on hibernate configurations (Mapping using hbm.xml). But am wondering if its rather better to use EJB3_Hibernate annotations instead of the XML mappings since am not that experienced with Hibernate tools... Having said that, i have a specific questions on annotations.

My current POJOs are used to contain data copied from a form bean (struts). These objects can be made annotated to become persistent to my DB tables. But they do not have all the properties contained on the table. For an example, i have an object called Members

Code:
public abstract class Members implements java.io.Serializable  {
   
   private String initials;
   private String surname;
   private String force;
   private String qualCode;
   private String qualName;
   
   public Members() {}
   
   public Members(String initials, String surname, String force,
         String qualCode, String qualName) {
      super();
      this.initials = initials;
      this.surname = surname;
      this.force = force;
      this.qualCode = qualCode;
      this.qualName = qualName;
   }
   
   public String getSurname() {
      return surname;
   }
   public void setSurname(String surname) {
      this.surname = surname;
   }
   public String getForce() {
      return force;
   }
   public void setForce(String force) {
      this.force = force;
   }

   public String getInitials() {
      return initials;
   }

   public void setInitials(String initials) {
      this.initials = initials;
   }

   public String getFullName() {
      return  force + " "
            + surname + ", "
            + initials;   
   }

   public String getQualCode() {
      return qualCode;
   }

   public void setQualCode(String qualCode) {
      this.qualCode = qualCode;
   }

   public String getQualName() {
      return qualName;
   }

   public void setQualName(String qualName) {
      this.qualName = qualName;
   }   
}

/* HERE ... */

public class MembersDTO extends Members {
   
   public MembersDTO() {

   }

   public MembersDTO(String initials, String surname, String force,
         String qualCode, String qualName) {
      super(initials, surname, force, qualCode, qualName);

   }
}


But the actual table (MEMBERS) on the database has a lot more other properties and does not have few other properties included here (qualCode & qualName) ...

Q.

1. Is it a Requirement for a POJOs to be a persistent (using annotations/hbm.XML) to have ALL the properties in the DB table? Or by just adding @Column to only the matching properties this can still work?

2. As you can see, getFullName() doesn't return a property, would this be allowed in an entity bean?

3. Can an abstract class like i have be annotated? or should i do this on the child class MembersDTO?

Hope my questions are not confusing...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 8:55 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
1. Is it a Requirement for a POJOs to be a persistent (using annotations/hbm.XML) to have ALL the properties in the DB table? Or by just adding @Column to only the matching properties this can still work?


You don't have to map all db columns as long as the db accept null values in those columns or the db is able to generate a default value by itself.

Quote:
2. As you can see, getFullName() doesn't return a property, would this be allowed in an entity bean?


Yes it is allowed, but you need to annotate it with @Transient. See http://www.hibernate.org/hib_docs/annot ... g-property

Quote:
3. Can an abstract class like i have be annotated? or should i do this on the child class MembersDTO?


Yes, the abstract class can be annotated if you use the @MappedSuperClass annotation. You also need to annotate the MemberDTO class with at least an @Entity annotation. See http://www.hibernate.org/hib_docs/annot ... tml#d0e939


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 7:25 am 
Beginner
Beginner

Joined: Thu Sep 18, 2008 5:18 am
Posts: 28
nordborg wrote:
Quote:
1. Is it a Requirement for a POJOs to be a persistent (using annotations/hbm.XML) to have ALL the properties in the DB table? Or by just adding @Column to only the matching properties this can still work?


You don't have to map all db columns as long as the db accept null values in those columns or the db is able to generate a default value by itself.

Quote:
2. As you can see, getFullName() doesn't return a property, would this be allowed in an entity bean?


Yes it is allowed, but you need to annotate it with @Transient. See http://www.hibernate.org/hib_docs/annot ... g-property

Quote:
3. Can an abstract class like i have be annotated? or should i do this on the child class MembersDTO?


Yes, the abstract class can be annotated if you use the @MappedSuperClass annotation. You also need to annotate the MemberDTO class with at least an @Entity annotation. See http://www.hibernate.org/hib_docs/annot ... tml#d0e939


Thanks a lot nordborg, just your personal opinion. Do you prefer using the XML over annotations or vise-versa? Especially for an inexperience person like me!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 8:05 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
We are using XML mapping files. The main reason is that this was the only option when our project was started. Our first prototype used Hibernate 2, but we quickly migrated to Hibernate 3 as soon as a beta version was available. The annotations project was still in early alpha and lacked support for features that we needed.

If I start a new project today, I would probably use annotations, beacuse they are a lot easier to maintain when you don't need to keep mapping files synchronized with code.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 9:21 am 
Beginner
Beginner

Joined: Thu Sep 18, 2008 5:18 am
Posts: 28
nordborg wrote:
We are using XML mapping files. The main reason is that this was the only option when our project was started. Our first prototype used Hibernate 2, but we quickly migrated to Hibernate 3 as soon as a beta version was available. The annotations project was still in early alpha and lacked support for features that we needed.

If I start a new project today, I would probably use annotations, beacuse they are a lot easier to maintain when you don't need to keep mapping files synchronized with code.


That's what i thought when i had a first look at them. BTW, i'm trying to figure the difference between EJB3 annotations and those of Hibernate ... I'm using an eclipse/MyEclipse reverse engineering to to generate EJB3 POJOs and daos ... Since eclipse does not have reverse engineering for Hibernate annotations, how possible to use the EJB3 annotated POJOs with hibernate DAO classes?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 9:38 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
As far as I know you are required to use EJB3 annotations. It is only if you need to use Hibernate-specific features that you should use the Hibernate annotations.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 10:17 am 
Beginner
Beginner

Joined: Thu Sep 18, 2008 5:18 am
Posts: 28
nordborg wrote:
As far as I know you are required to use EJB3 annotations. It is only if you need to use Hibernate-specific features that you should use the Hibernate annotations.


Someone told me to use EJB at all, you are required to use EJB container .. I'm currently using tomcat ... :( ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 10:36 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I am pretty sure that you can use EJB3 annotations with Tomcat. For other things (such as EntityManager) I am not sure. Some more information is available here: http://www.hibernate.org/397.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 5:09 pm 
Newbie

Joined: Tue Nov 01, 2005 2:00 pm
Posts: 10
Yes, you can use the annotations in Tomcat. You aren't really creating EJBs, just telling hibernate the info it needs.


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