FAndrew FAndrew - 4 months ago 5x
Java Question

Hibernate - How to set an object which has pk and fk to be VARCHAR in database?

I have two entities, the first of which is:

public class E_CMS_CFG extends BaseEntity{

@OneToOne(cascade=CascadeType.ALL)//, fetch = FetchType.EAGER)
@JoinColumn(name = "CFG_TYPE", nullable = false, referencedColumnName = "CFG_TYPE", columnDefinition = "VARCHAR(32)"),
@JoinColumn(name = "CFG_TYPE_ID", nullable = false, referencedColumnName = "ID")
private E_CMS_CFG_TYPE cfgType;

The second entity is:

public class E_CMS_CFG_TYPE extends BaseEntity{

@Column(name = "CFG_TYPE", length = 32)
private String cfgType;

In the first entity, I use
columnDefinition = "VARCHAR(32)"
on the
column; however, in the database, it is creating a
integer column. Is it possible to create a
VARCHAR cfg_type
column and not an integer? And if it is, how? I am using Sqlite version.

The link did not help, but if I replace in
@NaturalId (mutable = false)
, it works, but it creates it without a primary key.


You could try to use the natural id annotation here. For example:

@NaturalId (mutable = false)
@Column(name = "CFG_TYPE", unique = true, nullable = false, length = 32)
private String cfgType;

But I think you would still have to provide an id property and that would still be an integer value/column in your database.

I think this Question is a duplicate of "how to use id with string type in jpa hibernate"

I hope that helps!