Solved

Hibernate problem

Posted on 2010-11-13
5
359 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 298
Android development question 2 79
JAVA API design with micro service cloud in mind 1 78
Java 8 to Java 6 8 33
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

749 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