Solved

Hibernate problem

Posted on 2010-11-13
5
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

717 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