Hi all !
I come to you with a quite annoying problem.
Ok so let's set up the background.
I'm trying to map an association between two objects : a Vehicle an a Component, a vehicle having possibly several components of different types (i show only two in the sample code below).
My Vehicle class is like that :
Code:
@Entity
@Table(name = "VEHICLE")
public class Vehicle extends UUIDBaseEntity {
/** The vehicle color. */
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="COLOR_ID")
private Component color;
/** The vehicle brand. */
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="BRAND_ID")
private Component brand;
/**
* @return the color
*/
@Column(name="COLOR_ID", columnDefinition="VARCHAR(36)")
public Component getColor() {
return color;
}
/**
* @param color the color to set
*/
public void setColor(Component color) {
this.color = color;
}
/**
* @return the brand
*/
@Column(name="BRAND_ID", columnDefinition="VARCHAR(36)")
public Component getBrand() {
return brand;
}
/**
* @param brand the brand to set
*/
public void setBrand(Component brand) {
this.brand = brand;
}
}
Here is why i put the annotations above.
- @JoinColumn, because the objects are supposed to be persisted in an existing table, and the vehicle is linked to its components through components ids (here brand_id and color_id).
- @Column on the getter defining two elements :
- The column name : because for testing i use H2 database having hibernate creating the schema before the tests, and if i don't add the @Column annotation on the getter, then the column name is called Brand or Color, based on the getter/setter names i guess. But i need it to be brand_id and color_id, to be able to fit existing database and to use direct SQL.
- The column definition, as a varchar(36) (uuid) because if i don't add this, the column is defined as a varbinary in H2 by hibernate, so direct sql searches on the uuid as a string does not work. And as i expect my persistence in production to be in the existing database with a schema not managed by hibernate, with varchar2(36) uuids, i have to use the same schema for testing.
The component class is straightforward, mapped in its own table, with default column names and no association.
So my problems are :
- Why hibernate does not use JoinColumn name for column naming (brand_id for example) ?
- Is it normal to have to use Column on the getter to force this? And is there a conflict between JoinColumn and Column?
- Why is hibernate using varbinary by default ?
- Finally, when i try to retrieve vehicles from hibernate, i get an error : org.hibernate.type.SerializationException: could not deserialize, caused by : java.io.StreamCorruptedException: invalid stream header: 7B3D055A. Wether i let hibernate use its varbinary or i force varchar(36), same result, only the number (7B3D055A) changes. Can someone show me what's wrong? I must say i debugged, and hibernate fails on the first component (brand_id).
I would really appreciate any help...
Thanks !