I read this...
http://hibernate.org/42.html to no avail. If anyone knows steps to debug a problem like this, that would be GREAT TOO (then instead of fishing for me, I can learn how to catch my own fish)
Description, I have a db model of
order has an address, shirt and user(all one to many)
shirt has a user(designer, again one to many)
address has a user(again one to many..user can have many addresses)
My problem exists with the following code
mgr.persist(addressDbo);
mgr.persist(shirtDbo);
mgr.persist(orderDbo);
order has a shirt and address(they are not null) and when it persists the address, I get "not-null property references a null or transient value: com.joloft.impl.tshirt.db.OrderDbo.shirt"
If I switch the order
mgr.persist(shirtDbo);
mgr.persist(addressDbo);
mgr.persist(orderDbo);
I get "not-null property references a null or transient value: com.joloft.impl.tshirt.db.OrderDbo.address"
What I am really confused about is why persist fails...shouldn't the commit fail instead if there was not integrity with the transaction. I mean, if I run the same SQL in postgres between transaction open and close, I can then commit the transaction and everything works. With EJB3/hibernate though, it seems I can't commit this. I will have to turn my integrity off by having shirt property on orderDbo be nullable=true which is definitely not what I want. I keep running into this every time I start to use hibernate and then usually hack away the integrity(which is not the correct solution of course). Any ideas? Full detail below....
**************************************************
FULL DETAIL BELOW
**************************************************
Hibernate version:
3.2
Mapping documents:
anyways to spit this out from annotations here is the summary though
orderDbo.....
@ManyToOne
@JoinColumn(nullable=false)
public UserDbo getBuyer()
@ManyToOne
@JoinColumn(nullable=false)
public AddressDbo getAddress()
@ManyToOne
@JoinColumn(nullable=false)
public ShirtDbo getShirt()
addressDbo....
@ManyToOne
@JoinColumn(nullable=false)
public UserDbo getUser()
@OneToMany(mappedBy = "address", cascade = CascadeType.ALL)
protected List<OrderDbo> getOrders()
shirtDbo
@ManyToOne
@JoinColumn(nullable=false)
public UserDbo getDesigner()
@OneToMany(mappedBy = "shirt", cascade = CascadeType.ALL)
protected List<OrderDbo> getOrders()
userDbo
@OneToMany(mappedBy="designer", cascade = CascadeType.ALL)
protected List<ShirtDbo> getDesignedShirts()
@OneToMany(mappedBy="user", cascade = CascadeType.ALL)
protected List<AddressDbo> getAddresses()
@OneToMany(mappedBy = "buyer", cascade = CascadeType.ALL)
protected List<OrderDbo> getOrders()
Code between sessionFactory.openSession() and session.close():
Code:
Shirt shirt = order.getShirt();
Session session = SessionThreadLocal.getSession();
UserDbo user = (UserDbo) session.get(TshirtSvcImpl.USER_KEY);
log.info("SUCCESS IN PLACING YOUR FIRST ORDER");
log.info("addr="+order.getAddress());
ShirtDbo shirtDbo = new ShirtDbo();
shirtDbo.setPathToFile(order.getShirt().getFrontImagePath());
shirtDbo.setProduct(Product.valueOf(shirt.getProduct()));
shirtDbo.setSex(Sex.valueOf(shirt.getSex()));
shirtDbo.setStyle(Style.valueOf(shirt.getStyle()));
shirtDbo.setColor(Color.valueOf(shirt.getColor()));
AddressDbo addressDbo = new AddressDbo();
addressDbo.setProvince(order.getAddress().getProvince());
addressDbo.setCity(order.getAddress().getCity());
addressDbo.setStreet(order.getAddress().getStreetAddress());
addressDbo.setPostCode(order.getAddress().getZipcode());
OrderDbo orderDbo = new OrderDbo();
orderDbo.setQuantity(order.getQuantity());
orderDbo.setProduct(shirtDbo.getProduct());
orderDbo.setSex(shirtDbo.getSex());
orderDbo.setStyle(shirtDbo.getStyle());
orderDbo.setColor(shirtDbo.getColor());
orderDbo.setSize(Size.valueOf(shirt.getSize()));
TxInfo info = DbThreadLocal.instance().getEntityMgr();
EntityManager mgr = info.getEntityMgr();
user = mgr.merge(user);
user.addOrder(orderDbo);
user.addAddress(addressDbo);
user.addShirtDesign(shirtDbo);
addressDbo.addOrder(orderDbo);
shirtDbo.addOrder(orderDbo);
mgr.persist(orderDbo);
mgr.persist(shirtDbo);
mgr.persist(addressDbo);
session.set(USER_KEY, user);
Full stack trace of any exception that occurs:Name and version of the database you are using:PostgreSQL 8.2
The generated SQL (show_sql=true):Hibernate:
select
userdbo0_.Id as Id1_1_,
userdbo0_.version as version1_1_,
userdbo0_.password as password1_1_,
userdbo0_.email as email1_1_,
userdbo0_.phoneNumber as phoneNum5_1_1_,
orders1_.buyer_Id as buyer11_3_,
orders1_.Id as Id3_,
orders1_.Id as Id3_0_,
orders1_.address_Id as address12_3_0_,
orders1_.size as size3_0_,
orders1_.version as version3_0_,
orders1_.quantity as quantity3_0_,
orders1_.shirt_Id as shirt10_3_0_,
orders1_.product as product3_0_,
orders1_.sex as sex3_0_,
orders1_.style as style3_0_,
orders1_.color as color3_0_,
orders1_.orderDate as orderDate3_0_,
orders1_.buyer_Id as buyer11_3_0_
from
TshirtUsers userdbo0_
left outer join
TshirtOrders orders1_
on userdbo0_.Id=orders1_.buyer_Id
where
userdbo0_.Id=?
Hibernate:
select
addresses0_.user_Id as user8_1_,
addresses0_.Id as Id1_,
addresses0_.Id as Id2_0_,
addresses0_.name as name2_0_,
addresses0_.version as version2_0_,
addresses0_.street as street2_0_,
addresses0_.city as city2_0_,
addresses0_.province as province2_0_,
addresses0_.postCode as postCode2_0_,
addresses0_.user_Id as user8_2_0_
from
TshirtAddresses addresses0_
where
addresses0_.user_Id=?
Hibernate:
select
designedsh0_.designer_Id as designer8_1_,
designedsh0_.Id as Id1_,
designedsh0_.Id as Id0_0_,
designedsh0_.version as version0_0_,
designedsh0_.pathToFile as pathToFile0_0_,
designedsh0_.product as product0_0_,
designedsh0_.sex as sex0_0_,
designedsh0_.style as style0_0_,
designedsh0_.color as color0_0_,
designedsh0_.designer_Id as designer8_0_0_
from
Tshirts designedsh0_
where
designedsh0_.designer_Id=?
Hibernate:
select
nextval ('hibernate_sequence')
2008-2-9 2:26:22 com.joloft.impl.tshirt.TshirtSvcProxy2 invoke
信息: done invoking method=public abstract void com.joloft.api.tshirt.TshirtSvc.placeOrder(com.joloft.api.tshirt.Order)
2008-2-9 2:26:22 biz.xsoftware.mock.testcase.MockTestCase runBare
警告: TEST FAILED=testPlacingOrder
Debug level Hibernate log excerpt:[/code]