Hibernate version:3.1b5 + 3.1b3 annotations
I'm encountering a problem with using a base entity (assigned using @EmbeddableSuperclass) in a @ManyToOne relationship. Long story short: When attempting to save, it is failing due to a null entry being saved to the fk column. It seems it is failing to pick up the id of the FK class (which uses the base entity)....when I remove the base entity from the hierarchy and place the id methods in the fk class itself, it finds the id and saves the otm/mto just fine.
Again, this works when not using an @EmbbedableSuperclass
Usage
Code:
...
Company c = new Company();
c.setId(1);//there is a company record with this id in the database
Address a = new Address();
a.setCity("Norman");
a.setState("OK");
a.setStreet1("123 Main St.");
a.setZip("73072");
a.setCompany(c);
dao.save(a);
...
Note param 3Code:
...
15:04:51.484 DEBUG (org.hibernate.jdbc.AbstractBatcher:324) - insert into Address (OPT_LOCK, city, company_fk, street1, street2, zip, state, id) values (?, ?, ?, ?, ?, ?, ?, null)
Hibernate: insert into Address (OPT_LOCK, city, company_fk, street1, street2, zip, state, id) values (?, ?, ?, ?, ?, ?, ?, null)
15:04:51.484 DEBUG (org.hibernate.jdbc.AbstractBatcher:377) - preparing statement
15:04:51.484 DEBUG (org.hibernate.persister.entity.BasicEntityPersister:1671) - Dehydrating entity: [com.sonicdrivein.portfolio.domain.model.Address#<null>]
15:04:51.484 DEBUG (org.hibernate.type.NullableType:60) - binding '0' to parameter: 1
15:04:51.484 DEBUG (org.hibernate.type.NullableType:60) - binding 'norman' to parameter: 2
15:04:51.484 DEBUG (org.hibernate.type.NullableType:53) - binding null to parameter: 3
15:04:51.484 DEBUG (org.hibernate.type.NullableType:60) - binding '207 harvard' to parameter: 4
15:04:51.500 DEBUG (org.hibernate.type.NullableType:53) - binding null to parameter: 5
15:04:51.500 DEBUG (org.hibernate.type.NullableType:60) - binding '73072' to parameter: 6
15:04:51.500 DEBUG (org.hibernate.type.NullableType:60) - binding 'OK' to parameter: 7
...
Company (One side of the MTO/OTM)
Code:
@Entity
public class Company extends AbstractEntity{
// ------------------------------ FIELDS ------------------------------
private Set<Address> addresses;
private String name;
private String symbol;
// --------------------------- CONSTRUCTORS ---------------------------
public Company() {
super();
}
public Company(String name, String symbol) {
this();
this.name = name;
this.symbol = symbol;
}
// --------------------- GETTER / SETTER METHODS ---------------------
@OneToMany(mappedBy = "company")
public Set<Address> getAddresses() {
return addresses;
}
public String getName() {
return name;
}
@Column(unique = true)
public String getSymbol() {
return symbol;
}
...
}
Address (Many side of the MTO/OTM)
Code:
@Entity
public class Address extends AbstractEntity {
// ------------------------------ FIELDS ------------------------------
private Company company;
private String city;
private String state;
private String street1;
private String street2;
private String zip;
// --------------------------- CONSTRUCTORS ---------------------------
public Address() {
}
public Address(String zip, String street1, String state, String city) {
this.zip = zip;
this.street1 = street1;
this.state = state;
this.city = city;
}
// --------------------- GETTER / SETTER METHODS ---------------------
public String getCity() {
return city;
}
@ManyToOne
@JoinColumn(name="company_fk")
public Company getCompany() {
return company;
}
...
}
AbstractEntity
Code:
@EmbeddableSuperclass
public abstract class AbstractEntity implements Serializable {
// ------------------------------ FIELDS ------------------------------
private Integer id;
private Integer version;
// --------------------- GETTER / SETTER METHODS ---------------------
@Id(generate = GeneratorType.IDENTITY)
public Integer getId(){
return id;
}
@Version
@Column(name = "OPT_LOCK")
public Integer getVersion() {
return version;
}
public void setId(Integer id){
this.id = id;
}
public void setVersion(Integer version) {
this.version = version;
}
}
DDL
Code:
create table Address (
id integer generated by default as identity (start with 1),
OPT_LOCK integer,
city varchar(255),
street1 varchar(255),
street2 varchar(255),
zip varchar(255),
state varchar(255),
company_fk integer,
primary key (id)
);
create table Company (
id integer generated by default as identity (start with 1),
OPT_LOCK integer,
name varchar(255),
symbol varchar(255),
primary key (id),
unique (symbol)
);
alter table Address
add constraint FK1ED033D42684B579
foreign key (company_fk)
references Company;