Hibernate Core version: 3.2.5.GA
Hibernate Annotations version: 3.3.0.GA
Problem Description:
As you might have guessed from the title, I'm having some issues with persisting an annotated bidirectional one-to-one association using shared primary keys. Both the parent and child extend an abstract domain object that generates a unique Id upon construction. The problem seems to be with the proper substitution of the id on the child entity with that of the parent in the relationship. Instead of the parent's id being used when cascading the insert of the child, the application generated id of the child is used which violates a foreign key constraint when the session is flushed. It seems to work if I explicitly override the id of the child with that of the parent ( child.setId(parent.getId()) ) but I was under the impression that this would be handled automatically, assuming the classes are annotated correctly.
Abstract Parent Class:
Code:
@MappedSuperclass
public abstract class AbstractDomainObject implements Serializable {
protected String id;
protected Integer version;
public AbstractDomainObject() {
setId(UUIDGenerator.getInstance().generateRandomBasedUUID().toString());
}
...
}
Parent Entity:Code:
@Entity
public class Stock extends AbstractDomainObject {
private String symbol;
private Company company;
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
public Company getCompany() {
return company;
}
...
}
Child Entity:Code:
@Entity
public class Company extends AbstractDomainObject {
private String name;
private String url;
private Stock stock;
@OneToOne(mappedBy = "company")
public Stock getStock() {
return stock;
}
...
}
I've also tried overriding the getId() method on the child class (Company) and using:
@GeneratedValue(generator = "foreign")
@GenericGenerator(name = "foreign", strategy = "foreign", parameters = {@Parameter(name = "property", value = "stock")})
but to no avail. Anyone know what's up with this? I've looked at a couple of other posts (one which reported this as a possible bug in an old version of hibernate annotations but has since been marked as fixed), but can't seem find exactly what I'm looking for.
Thanks for the help ...
Code between sessionFactory.openSession() and session.close(): Code:
Stock stock = new Stock();
stock.setSymbol("symbol");
stock.setCompany(new Company());
stock.getCompany().setName("companyName");
stock.getCompany().setUrl("companyUrl");
stock.getCompany().setStock(stock);
stockDao.save(stock);
Full stack trace of any exception that occurs:
2007-08-14 13:09:03,479 ERROR hibernate.util.JDBCExceptionReporter::logExceptions(78) - Cannot add or update a child row: a foreign key constraint fails (`company`, CONSTRAINT `FK_company_1` FOREIGN KEY (`id`) REFERENCES `stock` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Name and version of the database you are using: MySQL 5.0
The generated SQL (show_sql=true):
2007-08-14 13:09:03,432 DEBUG org.hibernate.SQL::log(401) - insert into Stock (version, symbol, id) values (?, ?, ?)
2007-08-14 13:09:03,463 DEBUG hibernate.type.IntegerType::nullSafeSet(133) - binding '0' to parameter: 1
2007-08-14 13:09:03,463 DEBUG hibernate.type.StringType::nullSafeSet(133) - binding 'symbol' to parameter: 2
2007-08-14 13:09:03,463 DEBUG hibernate.type.StringType::nullSafeSet(133) - binding 'bda4ffb2-9b07-4d11-806b-03cee222d85e' to parameter: 3
2007-08-14 13:09:03,463 DEBUG org.hibernate.SQL::log(401) - insert into Company (version, name, url, id) values (?, ?, ?, ?)
2007-08-14 13:09:03,463 DEBUG hibernate.type.IntegerType::nullSafeSet(133) - binding '0' to parameter: 1
2007-08-14 13:09:03,463 DEBUG hibernate.type.StringType::nullSafeSet(133) - binding 'companyName' to parameter: 2
2007-08-14 13:09:03,463 DEBUG hibernate.type.StringType::nullSafeSet(133) - binding 'companyUrl' to parameter: 3
2007-08-14 13:09:03,463 DEBUG hibernate.type.StringType::nullSafeSet(133) - binding '9ac37200-ac93-4cb2-b953-5c928c25d07c' to parameter: 4