-->
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.  [ 5 posts ] 
Author Message
 Post subject: What is the common way to write persistent domain classes?
PostPosted: Tue Aug 25, 2009 11:51 pm 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
What is the common way to write persistent domain classes with hibernate these days? Let's assume we have a project with 100 domain classes. Use a hibernate xml? What about annotations? What ends up being the best/light-weight route to take?

Also, as an aside, what are the common ways to test databases these days? I used to use dbunit, but I found this to be too much work. A necessary evil?

I really want to make applications as fast as my mind thinks (good luck with that one). If there's something that can get me there the closest without having to use grails or something like that, I'd love to hear about it.

Thanks!


Top
 Profile  
 
 Post subject: Re: What is the common way to write persistent domain classes?
PostPosted: Wed Aug 26, 2009 8:49 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Check out the Hibernate Tools project. It has some great facilities for reverse engineering hbm files from Java classes - probably your best best for fast generation.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: What is the common way to write persistent domain classes?
PostPosted: Wed Aug 26, 2009 10:00 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
Thanks for the reply!

That sounds like a good idea. I already have the domain classes outlined (all the fields).

I heard that people are moving to an ejb3 aproach with spring now. Is that a good solution, or is using the xml mapping file still the best way?


Top
 Profile  
 
 Post subject: Re: What is the common way to write persistent domain classes?
PostPosted: Wed Aug 26, 2009 10:50 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I'm a big fan of annotations. (I wrote a book about Hibernate and JPA Annotations, so I'm biased.)

Still, with a large number of domain classes, there is something to be said to being able to go into just one file and making the appropriate changes, and not have to open up twenty different Java classes and recompile them. So, it's really a matter of just using the best approach for the job.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: What is the common way to write persistent domain classes?
PostPosted: Wed Aug 26, 2009 10:59 pm 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
I've been playing with annotations. I like much about them, although embedded objects tend to get a big bloated with all the overriding information.

I am having some trouble with one thing. I have a DomainObject base class that all my objects extend from. This is a common pattern I've used for years. I have used @MappedSuperclass on DomainObject to allow Hibernate to read my properties, and that works. However, I am getting a weird error for the name of the "id" column. It thinks that I am repeating it.

If I get rid of the id in DomainObject and put it in my subclass directly, and then name the column from "id" to "my_class_id", the error goes away.

Is there any way to have the methods and everything in the base class, but then specialize the name of the identifier property in the subclass, or do I need to repeat the id in every domain object?

The reason this is important is that I have some methods in DomainObject that act on the ID... and I guess I'd rather operate on the private variable than abstract property methods.

Code:
@MappedSuperclass
public abstract class DomainObject implements Serializable {

   /* Members */
   @Id
   @GeneratedValue
   @Column( name = "id" )
   protected Long id;

   @Column( name = "is_active" )
   protected boolean isActive = true;

   /* Constructors */

   /* Services */
   public boolean isTransient() {
      return getId() == 0;
   }

   /* Properties */
   public Long getId() {
      return id;
   }

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

   public boolean isActive() {
      return isActive;
   }

   public void setActive( boolean active ) {
      isActive = active;
   }
}


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