Solved

Hibernate problem

Posted on 2010-11-13
5
355 Views
Last Modified: 2012-05-10
Below is a template of my code. Many orders can be placed by a customer but a customer can only be inserted once. My senario works if there is a new customer placing an order but it fails when a customer already exist in the DB and i try to place a new order for that customer.

Problem is that if there is a new order i try to see if a customer already exist in the DB. If it is then all i need to do then is place the order Id with the existing customerId in order_info table but instead of trying to insert it always treats as an update meaning no new row inserted in order_info table. My OrderInfo class extends Customer class.For getting the id i did like this :-

Customer cust = hibernateTemplate.get(Customer.class,customerId);

This tells me that i do have a customer id already associated to a customer so now i want to add a new record in order_info table with the existing customerId but it treats as an update
Customer



@Entity

@Table(name="customer")

public class Customer {



private String customerId;



private String name;



private String address;



@Id

@GeneratedValue(strategy=GenerationType.AUTO)

@Column(name="customer_id")

public String getCustomerId() {

return customerId;

}

.

.

//more getter and setters for those fields



}





@Entity

@Table(name="order_info")

public class OrderInfo extends Customer {



private Order order;



@OneToOne(fetch=FetchType.LAZY)

@JoinColumn(name="order_id")

public Order getOrder() {

return order;

}



order_info table have 2 columns :-

order_id FK

customer_id FK



@Entity

@Table(name="order")

public class Order {



private OrderInfo orderInfo;



private String orderId;



@Id

@GeneratedValue(strategy=GenerationType.AUTO)

@Column(name="order_id")

public String getOrderId() {

return orderId;

}





@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="order")

public String getOrderInfo() {

return orderInfo

}



//Some other getters and setters

Open in new window

0
Comment
Question by:Micheal_Male
  • 3
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
AngryBinary earned 250 total points
ID: 34129362
I think the issue may be in the design. The OrderInfo class should not be an extension of Customer. Instead, it should have a Customer property and an Order property.

Alternately, you can set up the Customer class to contain a collection of OrderInfo, or (if there is a one-to-one relationship between Order and OrderInfo), cut out the OrderInfo entity altogether, and link the Order entity directly as a child of Customer.
0
 

Author Comment

by:Micheal_Male
ID: 34132020
I've made changes to the code and everything is working fine now. I've removed OrderInfo extends Customer and added an OrderInfo set in the Customer class. I need to ask a simple question now :-

If i add this in the named query :-

Select order from OrderInfo order

So i am assuming with the annotation i have FetchType.LAZY or FetchType.EAGER when that query executes then my orders will also be loaded in the session. Unfortunately when i do this :-

//Execute the namedquery
List<OrderInfo> info = hibernateTemplate.fetch(NamedQuery);

when i print the list size i get actualy the same result of columns what are in the DB. so far so good but when i do this :-

for(OrderInfo o : info) {
System.out.println(o.getOrder().getOrderId());
}

I get a NullPointerException over here. Any idea why ? do i have to write a query in order to retrieve the orders also cause i thought hibernate will load appropriate childs in the session,
0
 

Author Comment

by:Micheal_Male
ID: 34132652
I figure it out why it was doing that. if i load the Customer then all it's childs gets loaded also. However, if i load the OrderInfo then only OrderInfo is loaded not the order and the customer. Don't know why hibernate treat it like that
0
 
LVL 13

Expert Comment

by:AngryBinary
ID: 34141937
I would imagine it's because Customer isn't considered a child of OrderInfo. If it recursively fetched every reference object, loading one object could potentially be an extremely heavy operation (imagine the implication with a social networking data model).
0
 

Author Closing Comment

by:Micheal_Male
ID: 34184186
Thanks Angry. My previous solution and your suggestions did work for me. It looks like everytime i had to fetch data i would have to load the  parent in order for it's child entities to be loaded also which seems reasonable for me instead of me writing queries.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now