FAndrew FAndrew - 6 months ago 7
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:

@Entity
@Table(name="E_CMS_CFG")
public class E_CMS_CFG extends BaseEntity{

@Id
@OneToOne(cascade=CascadeType.ALL)//, fetch = FetchType.EAGER)
@JoinColumns({
@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:

@Entity
@Table(name="E_CMS_CFG_TYPE")
public class E_CMS_CFG_TYPE extends BaseEntity{

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


In the first entity, I use
columnDefinition = "VARCHAR(32)"
on the
CFG_TYPE
column; however, in the database, it is creating a
cfg_type
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 3.8.10.1 version.

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

Answer

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!

Comments