Link to home
Create AccountLog in
Avatar of Micheal_Male
Micheal_MaleFlag for Afghanistan

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of AngryBinary
AngryBinary

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Micheal_Male

ASKER

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,
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
Avatar of AngryBinary
AngryBinary

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).
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.