-->
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.  [ 2 posts ] 
Author Message
 Post subject: Code critique / Suggestions for improvement?
PostPosted: Tue Dec 19, 2006 9:20 pm 
Newbie

Joined: Tue Dec 19, 2006 9:08 pm
Posts: 3
I am new to Hibernate and was hoping I can gather some feedback on using a lookup table. Reading posts in the forum, it seems like a simple ManyToOne mapping does the trick, however, I am not sure if my code (see below) can be improved upon. My main concern is that I'm not sure if an enumeration would be better; also, I hate hard-coding those ids but I can't think of any other way to do this.

Any feedback / suggestions would be appreciated.

Sample client code:

Code:
OrderStatus os = OrderStatus.INIT_STATUS;
Order order = new Order();
order.setOrderStatus(os);
manager.persist(order);


The "lookup" or "reference" data entity:

Code:
@Entity
public class OrderStatus implements Serializable {

    @Id
    private Long id;

    private String status;
   
    public final static OrderStatus INIT_STATUS = new OrderStatus(new Long(1), "Initialized");
    public final static OrderStatus PENDING_STATUS = new OrderStatus(new Long(2), "Pending");
    public final static OrderStatus CANCELLED_STATUS = new OrderStatus(new Long(3), "Cancelled");


    public OrderStatus(Long id, String status){
         this.id = id;
         this.status = status;
    }
    ... // other getters/setters not shown
}


The parent entity class, with a reference to one orderStatus entity:

Code:
@Entity
public class Order implements java.io.Serializable{
   
   
    private int orderId;
    private OrderStatus orderStatus;

    @ManyToOne
    public OrderStatus getOrderStatus() {
        return orderStatus;
    }

    ... // other getters/setters not shown
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 9:30 pm 
Newbie

Joined: Tue Dec 19, 2006 9:08 pm
Posts: 3
One thing I forgot to mention: I'm using Hibernate 3 on JBoss 4.0.5. I'm also trying to keep things close to the Java Persistence API spec so that my app can be deployed on both Glassfish & JBoss.


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