PDA

View Full Version : One to many mapping problem


pedjasmek
31-10-2009, 05:20 AM
In my JPA project I'm using these three classes:
Cart.java:

package com.spinnaker.pedja;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.*;

@<hidden>(name = "Cart")
@<hidden>(schema = "shop")
public class Cart {

@<hidden>
@<hidden>(name = "cart_id")
private int Cart_id;

@<hidden>(name = "cart_date")
private Date date;

@<hidden>(mappedBy = "cartItemPK", targetEntity = CartItemPK.class)
private Set cartitemCollection = new HashSet(0);

@<hidden>
@<hidden>(name = "customer_id")
private Customer customer;

public Customer getCustomer() {
return customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}

public Cart() {
}

public Set getCartitemCollection() {
return cartitemCollection;
}

public void setCartitemCollection(Set cartitemCollection) {
this.cartitemCollection = cartitemCollection;
}

public int getCart_id() {
return Cart_id;
}

public void setCart_id(int cart_id) {
this.Cart_id = cart_id;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}
}


, CartItem.java:

package com.spinnaker.pedja;

import java.io.Serializable;

import javax.persistence.*;

@<hidden>
@<hidden>(schema = "shop")
public class CartItem implements Serializable {


@<hidden>
@<hidden>
// @<hidden>(name = "cart_id")
CartItemPK cartItemPK;

@<hidden>(name = "quantity")
private int quantity;

@<hidden>(name = "unit_price")
private double unit_price;



// @<hidden>(name = "cart_id")
// private Cart cart;


// public Cart getCart() {
// return cart;
// }
//
// public void setCart(Cart cart) {
// this.cart = cart;
// }

public CartItem() {
}

public CartItemPK getCartItemPK() {
return cartItemPK;
}

public void setCartItemPK(CartItemPK cartItemPK) {
this.cartItemPK = cartItemPK;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public double getUnit_price() {
return unit_price;
}

public void setUnit_price(double unit_price) {
this.unit_price = unit_price;
}

}


and CartItemPK.java:

package com.spinnaker.pedja;

import java.io.Serializable;

import javax.persistence.*;


@<hidden>(schema="shop")
@<hidden>
public class CartItemPK implements Serializable{


@<hidden>(name="item_id",nullable=false)
private int itemId;



// @<hidden>
// @<hidden>(name = "cart_id")
// private Cart cart;

@<hidden>(name="cart_id",nullable=false)
private int cartId;

public int getItemId() {
return itemId;
}

public void setItemId(int itemId) {
this.itemId = itemId;
}

public int getCartId() {
return cartId;
}

public void setCartId(int cartId) {
this.cartId = cartId;
}


}
.
I'm having problem with mapping one to many relationship between Cart and CartItem.I had to introduce CartItemPK class because CartItem has composite primary key.Help please!!!

vseehua
31-10-2009, 07:36 AM
pedjasmek (http://www.recom.org/forum/member.php?u=14389), I think you may have come to the wrong forum for your querries. JPA, within the context of this forum, means Jabatan Perkhidmatan Awam in Malaysia, and usually usually used referring to the scholarships offered by that body.

As thus, I don't think many of us knows how to deal with Java Persistent API codes. I will leave this thread open, and see if anyone has the answers to your querries.

lookingaround
31-10-2009, 11:24 AM
Try go to this website. You can ask here.

http://forums.tizag.com/index.php