Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

Hibernate problem

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
Micheal_Male
Asked:
Micheal_Male
  • 3
  • 2
1 Solution
 
AngryBinaryCommented:
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
 
Micheal_MaleAuthor Commented:
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
 
Micheal_MaleAuthor Commented:
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
 
AngryBinaryCommented:
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
 
Micheal_MaleAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now