Hibernate and No Pk

Hibernate without Primary Key

  1. Hibernate requires that entity tables have primary keys. End of story.
  2. 50k records is simply not that many when you're talking about a database.

My advice: add an autoincrement integer PK column to the table. You'll be surprised at how fast it is.

Hibernate and no PK

I found that its not possible to do so. So bad luck for those working with legacy systems.

If you reverse engineer (create JPA annotated entities from existing JDBC connection) the table will create two Java classes, one Entity and with one field; id, and one embeddable id containing all the columns from your relation.

Hibernate Entity with no Primary Key and nullable fields

The best I managed to do here was to tweak my @Entity to include the ROWID pseudo-column. Unfortunately while HSQLDB supports a certain kind of ROWNUM it only works when NOT prefixed with the tableAlias, which Hibernate will always do. And it doesn't support ROWID at all. But ROWID which will work in Oracle won't work in HSQLDB, so the trickery is to manually setup my embedded HSQLDB with a ROWID column set to auto-increment with copious notes that this will Just Work(TM) on Oracle without modifying the model.

Yes, the correct answer is to add a real PK to the table in Oracle. Alternatively using an embedded Oracle (IE dockerized instance via test-containers) would work fine as well, but the licensing isn't particularly amenable...or so I'm told. BUT as hacky as it is to deliberately use a different schema in test than at runtime, it does work and is well-documented in multiple places so no one is surprised by anything.

Or...you know...just don't upgrade Hibernate. This entity snuck through a bug in older versions of Hibernate for YEARS where the Mapper accepted the multiple @Id fields as a composite key but the Loader didn't recognize the null field as part of the key so it allowed the object to rehydrate.

Hibernate mapping with none primary key table

According to Hibernate specification every entity should have Id, but if you don't have possibility to add Id to current table you still able to create class User without Entity annotation, and to map results of native query to this class.

Hibernate join using non-primary key column

JoinColumn will only specify the name of the column holding the foreign key, so changing the name of joincolumn absolutely will not help.

Hibernate will be using the primary key by default for joining, if you want to override this you can simply use referencedColumnName in your relation, but the referenced column should be unique

How to have No primary Key Entity using ManyToOne Relation to fetch data from mysql database

You cannot have Entities without primary key:

From the Spec:

2.4 Primary Keys and Entity Identity

Every entity must have a primary key.

The primary key must be defined
on the entity class that is the root of the entity hierarchy or on a
mapped superclass that is a (direct or indirect) superclass of all
entity classes in the entity hierarchy. The primary key must be
defined exactly once in an entity hierarchy.

Source: https://download.oracle.com/otn-pub/jcp/persistence-2_1-fr-eval-spec/JavaPersistence.pdf?AuthParam=1561040540_b447233fdfd994fdb2338dd9407c4977

So you must create a primary key of the fields of data.

If they are unique in combination you could create a composite key like this:

public class DataKey implements Serializable  {

@Id
private String ticket_no;

@Id
String type;

@Id
private String unit;

@Id
private float value;

// getters, setters, equals and hashCode implementations
}

@Entity
@Table(name="data")
@IdClass(DataKey.class)
public class Data {

@Id
@Column(name="ticket_no")
private String ticket_no;

@Id
@Column(name="type")
String type;

@Id
@Column(name="unit")
private String unit;

@Id
@Column(name="value")
private float value;

@ManyToOne
private Prescription pres;

//getters and setters

}

Hibernate Annotations Id for table with no primary key

If it is an entity type then you should use a composite key. This can be done by moving primary key fields to separate class and mapping that in entity class with an @Id annotation.

See Mapping composite primary keys and foreign keys to composite primary keys

If it is not an entity but a value type you should map it accordingly.
See https://stackoverflow.com/a/1696146/324900 and Entity and value types in hibernate



Related Topics



Leave a reply



Submit