-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: OneToOne and OneToMany error: null or transient objects
PostPosted: Thu Apr 27, 2006 10:59 am 
Beginner
Beginner

Joined: Wed Apr 26, 2006 2:41 pm
Posts: 30
Hibernate version: 3.2.0cr1
with ejb3 and annotations in a Resource Local config

Im having trouble expressing our db mapping in Entity Beans. We have the following tables:

Code:
products
  product_id (pk)
  product_master (fk)
  ...

product_masters
  product_master_id
  original_product_id (fk to products)
  ...


As you can see the product has a reference to the master. All products created in the system have masters although you may later switch the master it is pointing to. Neither products or masters are ever deleted from the system. The original product id should always point to the one that was inserted. This means that all of these relationships are not nullable.

Mapping documents:

Here's what I'm trying to do although I cant seem to get the right annotations for it to work:

Code:
@Entity
@Table(name = "products")
@SequenceGenerator(name = "products_seq", sequenceName="products_seq")
Product {

  private ProductMaster productMaster;
  private ProductMaster originalProductMaster;
  ...
  protected Product(){} 

  public Product() {
    ProductMaster master = new ProductMaster(this);
    setProductMaster(master);
    setOriginalProductMaster(master);
  }

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO, generator="products_seq")
  @Column(name = "product_id")
  public Long getProductId() {
    return productId;
  }

  ...

  @ManyToOne(cascade = CascadeType.PERSIST)
  @JoinColumn(name = "PRODUCT_MASTER_ID")
  public ProductMaster getProductMaster() {
    return productMaster;
  }

  ...

  @NotNull
  @OneToOne(fetch=FetchType.LAZY, cascade = CascadeType.PERSIST)
  @JoinColumn(name="product_id", updatable=false)
  public ProductMaster getOriginalProductMaster() {
    return originalProductMaster;
  }

  ...

}


Code:


@Entity
@Table(name = "product_master")
ProductMaster {
  private  Long productMasterId;
  private Product originalProduct;
  private List<Product> products;

  ...

  protected ProductMaster(){}

  public ProductMaster(Product p) {
    setOriginalProduct(p);
    products.add(p);
  }

  ...

  @Id
  @Column(name = "product_master_id")
  @GeneratedValue(strategy=GenerationType.AUTO, generator="product_masters_seq")
  public Long getProductMasterId() {
    return productMasterId;
  }
 
...
 
  @NotNull
  @ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.PERSIST)
  @JoinColumn(name="original_product_id", updatable=false)
  public Product getOriginalProduct() {
    return originalProduct;
  }

...
 
  @NotNull
  @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
  @JoinColumn(name = "PRODUCT_MASTER_ID")
  public List<Product> getProducts() {
    return products;
  }

  ....
}




Full stack trace of any exception that occurs:

Hibernate: select product_masters_seq.nextval from dual
Hibernate: select products_seq.nextval from dual
2006-04-27 10:13:46,437 [main] ERROR com.sonymusic.aoma.metadata.commands.ImportXMLCommand - javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.sonymusic.aoma.persistence.beans.Product.productMaster

...

Caused by: javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.sonymusic.aoma.persistence.beans.Product.productMaster
_____ 2006-04-27 10:13:46,453 [main] ERROR com.sonymusic.aoma.metadata.HotFolderImportDaemon - at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:562)
_____ 2006-04-27 10:13:46,453 [main] ERROR com.sonymusic.aoma.metadata.HotFolderImportDaemon - at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:193)

Name and version of the database you are using: Oracle 9


Any help is greatly appreciated


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 01, 2006 1:41 pm 
Beginner
Beginner

Joined: Wed Apr 26, 2006 2:41 pm
Posts: 30
The more I look at this, the more I don't understand how it can be done. Product and ProductMaster both have primary keys coming from sequences so they have @SequenceGenerator annotations to fill out their id's. ProductMaster has a reference to the product and the product has a reference to the product master.

All 4 columns have to be filled out when commiting the transaction. I would expect something like:

Code:
select product_seq.nextval from dual
select product_masters_seq.nextval from dual
insert into product_masters (product_master_id, original_product_id) values (123,789)
insert into products (product_id, product_master_id) values (789,123)


I've tried OneToOne, ManyToOne, and the Hibernate foreign key @GenericGenerator on the productMaster and I cant quite get it to work. (I may be doing it wrong since the documentation is a little sparse) The question I ran across is what to feed the @Parameter annotation. It looks like to use the foreign key generator i need to have a properly annotated @OneToOne getProduct() on that class.

Does anyone know how to solve this correctly?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.