Why is JPA choosing String for JoinColumn regardless of key types? Why is JPA choosing String for JoinColumn regardless of key types? postgresql postgresql

Why is JPA choosing String for JoinColumn regardless of key types?


I believe the issue is one of case sensitivity.

You did not set a @Column on the key id attribute, so the default column name is "KEY".In your @ManyToOne you referenced "key", which is not the same column, so EclipseLink (which is case sensitive and supports non Id foreign key references) assumed this was a different column, and one that it did not know about, so gave it the default type of VARCHAR.

Either change the referencedColumnName to "KEY" or remove it as it is not required when referencing a singleton Id.

It would be worthwhile to log a bug on EclipseLink that a warning should be logged when a column reference is not found, or has the wrong case (maybe even switch the case automatically). Actually we might be logging a warning already, you may wish to check your log.


Is it unrealistic for me to expect it to get the correct column type?

No, it's not unrealistic and the current result is clearly unexpected.

Am I using it wrong, or do I have the wrong expectations?

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 referencedColumnName that you should not have to define anyway)?

@Entity@Table( name = "contract" )@SequenceGenerator( name = "CONTRACT_SEQ_GEN", sequenceName = "CONTRACT_SEQUENCE" )public class Contract implements DataObject<Long> {    @Id    @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "CONTRACT_SEQ_GEN" )    private Long key;    ...    @ManyToOne( optional=false )    @JoinColumn( name="organization_key" )    private Organization organization;    ...}

I don't have PostgreSQL installed, can't try myself.

Is it expected for me to have to use the columnDefinition each time?

No.