Using the EclipseLink JPA2 implementation (not sure if it’s the same with the Hibernate implementation)
I have a simple structure where an Organization entity has contracts. Here’s the sql I exported from postgres to create the Organization
CREATE TABLE organization (
key bigint NOT NULL,
version integer
);
If I specify the Contract side of the Organization like this:
@ManyToOne( optional=false )
@JoinColumn( name="organization_key", referencedColumnName="key" )
private Organization organization;
and then dump the schema I get this.
CREATE TABLE contract (
key bigint NOT NULL,
version integer,
organization_key character varying(255),
);
It doesn’t make sense to me that it would use a character varying(255) field for the reference to the organization key. I know I can use the columnDefinition as follows:
@ManyToOne( optional=false )
@JoinColumn( name="organization_key", referencedColumnName="key", columnDefinition="bigint NOT NULL" )
private Organization organization;
to get the bigint type instead of the character type.
Is it unrealistic for me to expect it to get the correct column type? Am I using it wrong, or do I have the wrong expectations? Is it expected for me to have to use the columnDefinition each time?
Update:
Here’s the relevant info from the Organization entity
@Entity
@Table( name = "organization" )
@SequenceGenerator( name = "ORGANIZATION_SEQ_GEN", sequenceName = "ORGANIZATION_SEQUENCE" )
public class Organization
implements DataObject<Long>
{
/**
* key for this instance. Should be managed by JPA provider.
*/
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "ORGANIZATION_SEQ_GEN" )
private Long key;
/**
* JPA version column
*/
@Version
protected int version;
/**
* All contracts for this organization
*/
@OneToMany(mappedBy="organization" )
@OrderBy( "endDate DESC" )
private List<Contract> contracts;
... getters and setters
}
And here’s the Contract entity
@Entity
@Table( name = "contract" )
@SequenceGenerator( name = "CONTRACT_SEQ_GEN", sequenceName = "CONTRACT_SEQUENCE" )
public class Contract
implements DataObject<Long>
{
/**
* key for this instance. Should be managed by JPA provider.
*/
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "CONTRACT_SEQ_GEN" )
private Long key;
/**
* Organization that owns this contract, required.
*/
@ManyToOne( optional=false )
@JoinColumn( name="organization_key", referencedColumnName="key" )
private Organization organization;
/**
* JPA version column
*/
@Version
protected int version;
... getters and setters
}
No, it’s not unrealistic and the current result is clearly unexpected.
Your mappings doesn’t look wrong. Could you just try the following and confirm that you get the same result (I’m simply omitting the
referencedColumnNamethat you should not have to define anyway)?I don’t have PostgreSQL installed, can’t try myself.
No.