Hibernate looks for the wrong primary key - java

My entity has two foreign keys, one of those is the primary key. I read in the JPA Wiki that if the child's (Procurement) primary key is the same as the parent's (Article) one, you establish a #OneToOne relationship.
#SuppressWarnings("serial")
#Entity
public class Procurement implements Serializable {
#Id
#OneToOne
// the child's primary key is the same as the parent
#JoinColumn(name = "articleId", referencedColumnName = "id")
private Article article;
#OneToOne
#JoinColumn(name = "supplierId", referencedColumnName = "id")
private Supplier supplier;
Following the standard approach, the JpaRepository should look like this:
#Repository
public interface IProcurementRepository extends
JpaRepository<Procurement, Article>
However, if I want to call the findOne-method (which looks for primary key) delivering an "Article" object, Hibernate throws an exception.
org.hibernate.TypeMismatchException: Provided id of the wrong type for
class de.willms.spring.myerp.model.Procurement. Expected: class
de.willms.spring.myerp.model.Procurement, got class
de.willms.spring.myerp.model.Article
The DB table structure:
Table "article" (ID, shortText)
Table "supplier" (ID, name)
Table "procurement" (articleID, supplierID, price)
What do I have to change so that I can find a "Procurement" record by the corresponding "Article" object?

Finally, I found a special function in Hibernate (amazing, but hard to understand in the documentation) leading to a solution.
As soon as the primary key consists of several columns or, like in my case, relates to other tables, a special ID class has to be written.
#Embeddable
public class Procurement_ID implements Serializable {
/**
* This attribute establishes the 1:1 connection to a record in the
* "article" table. The column "articleId" is a foreign key to the column
* "id" in the {#link Article} entity. (Warning: This is Hibernate
* specific!)
*/
#OneToOne
#JoinColumn(name = "articleId", referencedColumnName = "id")
private Article article;
/**
* This attribute establishes the 1:1 connection to a record in the table
* "supplier". The column "supplierId" is a foreign key to the column "id"
* in the {#link Supplier} entity. (Warning: This is Hibernate specific!)
*/
#OneToOne
#JoinColumn(name = "supplierId", referencedColumnName = "id")
private Supplier supplier;
(Due to a more "normalised" data model, I switched to the composite primary key.)
Because of the #Embeddable annotation, this ID can be injected into the entity class.
#Entity
public class Procurement implements Serializable {
/**
* The composite primary key of the underlying table is defined in the
* {#link Procurement_ID} class.
*/
#EmbeddedId
private Procurement_ID procid;
With me using this approach, Hibernate inserts a new record with positive foreign key check:
[DEBUG] Generated identifier: component[article,supplier]{article=de.willms.spring.myerp.model.Article#1, supplier=de.willms.spring.myerp.model.Supplier#1}, using strategy: org.hibernate.id.CompositeNestedGeneratedValueGenerator
... (Flusing) ...
[DEBUG] Listing entities: ...
de.willms.spring.myerp.model.Procurement{price=2.5, deliveryTime=10,
procid=component[article,supplier]{article=de.willms.spring.myerp.model.Article#1,
supplier=de.willms.spring.myerp.model.Supplier#1}, priceUnit=$}
However, it is a pity that the JpaRepository cannot inject find()-method stubs concering only one part of the primary key. It is "unable to resolve the attribute against the path". You are welcome to comment!

You still have to use an id of type int or long. The difference is that instead of joining with #JoinColumn, you have to use #PrimaryKeyJoinColumn. You class should look like this.
#SuppressWarnings("serial")
#Entity
public class Procurement implements Serializable {
#Id
#GeneratedValue(generator = "articleForeignGenerator")
/* Hibernate-specific generator needed for shared primary key */
#GenericGenerator(name = "articleForeignGenerator", strategy = "foreign", parameters = #Parameter(name = "property", value = "article"))
#Column(name = "articleId")
private int articleId;
#OneToOne
// the child's primary key is the same as the parent
#PrimaryKeyJoinColumn
private Article article;
#OneToOne
#JoinColumn(name = "supplierId", referencedColumnName = "id")
private Supplier supplier;
Note that I use a hibernate-specific generator for the ID. It may be different for your JPA provider.

Related

Check whether exist the database object with the specified field values

I am new in Spring Data, and I need to establish the impossibility of creating a new entity in DB if an entity already exists with the same field values.
Comparison condition: if "closeType" field and "id" agreement field of a new entity equal to database entity fields, I can't add this entity to DB. How do it?
My entity:
#Entity
#Table(name = "contract")
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Contract implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "contractGenerator")
#SequenceGenerator(name = "contractGenerator", sequenceName = "contract_sequence")
private Long id;
#Column(name = "start_date")
private LocalDate startDate;
#Column(name = "end_date")
private LocalDate endDate;
#Column(name = "first_pay_date")
private LocalDate firstPayDate;
#Column(name = "next_pay_date")
private LocalDate nextPayDate;
//Here is thу first field for comparison
#Enumerated(EnumType.STRING)
#Column(name = "close_type")
private CloseType closeType;
#ManyToOne
#JsonIgnoreProperties("")
private Mentor mentor;
//Here is second ID agreement field for comparison
#ManyToOne
#JsonIgnoreProperties("")
private Agreement agreement;
...............
//getters and setters
I have to block possibility to create several active contracts("closeType") in one agreement ("id")
I have to block possibility to create several active
contracts("closeType") in one agreement ("id")
you could use UniqueConstraint How to introduce multi-column constraint with JPA annotations?
...
#Table(uniqueConstraints={
#UniqueConstraint(columnNames = {"close_type", "agreement"})
})
Contract implements Serializable {
...
}
Thanks, maybe you could hint me how to set constraint for "closeType"
if, for example, only closeType fields with Null value will be uniqe?
But other values of closeType wont be uniqe
How to annotate unique constraint with WHERE clause in JPA says:
creating partial indexes (CREATE INDEX ... ON ... WHERE) using JPA
aren't specified by JPA. You cannot use a unique constraint for this
purpose because unique partial indexes are not unique constraints.
Some JPA providers offer extension annotations specific to that JPA
provider that add features for running native DDL scripts, defining
indexes with annoations, etc. Since you haven't mentioned which JPA
provider you are using I can't tell you more. Here's the documentation
for EclipseLink index DDL;
I suggest you to have a look at the
How to annotate unique constraint with WHERE clause in JPA

Hibernate JPA, one to one relationship with a composite key and a constant value

I'm attempting to implement a limited type of object level ACL and its lead me to a place where I'm attempting to create a #OneToOne relationship using a composite key with a constant and dynamic value.
I have an Entity with a database id and a constant value defined in the class.
public class Entity{
private static final int objectType = 1;
#Id
Integer id;
}
I have an access_levels table with a composite key of objectId and objectType.
public class AccessLevel {
#EmbeddedId
private AccessLevelKey accessLevelKey;
#Embeddable
class AccessLevelKey implements Serializable{
private Integer objectType;
private Integer objectId;
....
}
}
Schema of access_levels
CREATE TABLE access_levels(
object_type INTEGER NOT NULL,
object_id INTEGER NOT NULL,
....
CONSTRAINT access_levels_type_id PRIMARY KEY (object_type, object_id)
);
I'm attempting to come up with a one to one relationship that Entity can use to fetch and update its associated AccessLevel
After taking a look a the docs on Non-Standard Joins it seems like I need something like this,
Inside of Entity:
#OneToOne
#JoinColumns({
#JoinColumn(name = "id", referencedColumnName = "object_id"),
#JoinColumn(name = "access_levels.object_type", referencedColumnName = "1"),
})
private AccessLevel accessLevel;
However this throws a hibernate MappingException at app launch
Caused by: org.hibernate.MappingException: Unable to find column with logical name: 1 in access_levels
Thanks!

HIbernate one-to-one annotation isn't generating foreign key GerericGenerator in dependent table

I am trying to create OneToOne relation between a Person and Auth table. The problem is when the DB table "Auth" is generated, I'm not seeing the foreign key in the AUTH table that should reference Person. The object is to have the Auth table use the same Primary Key of the Person Table.
#MappedSuperclass
public abstract class DomainBase {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
#Version
#Column(name="OPLOCK")
private Integer version;
}
#Entity
#Table(name = "person")
public class Person extends DomainBase {
#OneToOne(cascade=CascadeType.ALL)
#JoinColumn(name="auth_id")
private Auth auth;
}
#Entity
public class Auth {
#Id
#GeneratedValue(generator="foreign")
#GenericGenerator(name="foreign", strategy = "foreign", parameters={
#Parameter(name="property", value="person")
})
#Column(name="person_id")
private int personId;
---------------------------------
#OneToOne(cascade = CascadeType.ALL)
#PrimaryKeyJoinColumn
private Person person;
}
My Database scripts after hibernate DB generation.
CREATE TABLE auth
(
person_id integer NOT NULL,
activate boolean,
activationid character varying(255),
last_login_attempt_date timestamp without time zone,
last_login_attempt_timezone character varying(255),
last_login_date timestamp without time zone,
last_login_timezone character varying(255),
nonlocked boolean,
num_login_attempts integer,
CONSTRAINT auth_pkey PRIMARY KEY (person_id),
CONSTRAINT uk_d68auh3xsosyrjw3vmwseawvt UNIQUE (activationid)
)
WITH (
OIDS=FALSE
);
ALTER TABLE auth
OWNER TO postgres;
It seems that the problem is you declare twice the #OneToOne annotation between "person" table and "auth" table, without specify the relation between them. Take a look at the hibernate documentation, at the point 2.2.5.1, there is some examples about using one-to-one association.
For me, the best way is to set up the association in one table, the one that declare the foreing key column, and to use the mappedBy parameter in the other object. In your code, this will be :
#Entity
#Table(name = "person")
public class Person extends DomainBase {
#OneToOne(cascade=CascadeType.ALL)
#JoinColumn(name="auth_id")
private Auth auth;
}
#Entity
public class Auth {
#Id
#GeneratedValue(generator="foreign")
#GenericGenerator(name="foreign", strategy = "foreign", parameters={
#Parameter(name="property", value="person")
})
#Column(name="person_id")
private int personId;
#OneToOne(mappedBy = "auth")
private Person person;
....
}
This is the second example in the hibernate documentation, introduce just after the sentence "In the following example, the associated entities are linked through an explicit foreign key column". I tested this code, and the "auth_id" column appeared.

JPA #OneToMany join on part of a multipart key

A Profile table has a one to many association with a Privilege table. The privilege table has a multipart key, of a profile_id and a privilege_id. I want to join from the Profile table to the Privilege table only on the profile_id and get back a collection of privileges.
In my Profile class I have
#OneToMany(fetch = FetchType.LAZY)
#JoinColumn(name = "PROF_ID")
public List<ProfilePrivilegeEO> getProfilePrivileges()
{
return m_profilePrivileges;
}
My Privilege class has
private ProfilePrivilegeId m_profileId;
#EmbeddedId
public ProfilePrivilegeId getProfileId()
{
return m_profileId;
}
Where ProfilePrivilegeId is
#Embeddable
public class ProfilePrivilegeId
implements Serializable
{
private Integer m_profileId;
private Integer m_privNumber;
#Column(name = "PROF_ID")
public Integer getProfileId()
{
return m_profileId;
}
#Column(name = "PRIV_NUM")
public Integer getPrivNumber()
{
return m_privNumber;
}
.....
}
However, when i do that the static weaver says:
The #JoinColumns on the annotated element [method getProfilePrivileges] from the entity
class [class com.acme.ProfileEO] is incomplete. When the source entity class uses a
composite primary key, a #JoinColumn must be specified for each join column using the
#JoinColumns. Both the name and the referencedColumnName elements must be specified in
each such #JoinColumn.
However, the Profile table has no knowledge of privilege_ids... I don't see why JPA should demand that i specify both keys of the privilege table, that's just an arbitrary decision made by jpa with no valid reason why... What do i need to do to get this to work? (I am using EclipseLInk.)
Create an PrivilegeId class that encapsulate the ids. Make that class #Embedded and put it in the Privilege with #EmbeddedId.
In the PrivilegeId class, put an #OneToMany to the Profile and the privilege id.

Wrong SQL for view object using Hibernate Annotations

I'm working on a hibernate entity mapping for a database view; when I do a criteria query against it, hibernate is generating bad SQL. Any help figuring out what the problem is with my mapping would be greatly appreciated!
I have two mapped entities which I am trying to grab from a database view; the view has no other columns, just the FK of each entity. One of these FK's can be treated as a primary key, since the view has a row for each primary entity. So my DB schema for the view looks like:
primary(primary_id, some_other_fields)
history(history_id, primary_id, some_other_fields)
view_latest_status_history(primary_id, history_id)
Note the view is used because I want to pick out only the latest history for each primary, not all mapped history records. Here is the object I am using for the view, with entity annotations:
#Entity
#org.hibernate.annotations.Entity(dynamicUpdate = true)
#Table(name = "view_latest_status_history")
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class ViewLatestStatusHistoryRow implements Serializable {
private Primary primary;
private History history;
/**
* #return Returns the history.
*/
#ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, fetch = FetchType.LAZY)
#JoinColumn(name = "history_id", nullable = true)
#AccessType("field")
public History getHistory() {
return history;
}
//equals() and hashCode() implementations are omitted
/**
* #return Returns the primary.
*/
#Id
#ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, fetch = FetchType.LAZY)
#JoinColumn(name = "primary_id", nullable = false)
#AccessType("field")
public Primary getPrimary() {
return primary;
}
}
Both the Primary and History objects have complete, working entity annotations.
My criteria setup:
criteria.add(Restrictions.in("primary", [collection of primary objects]));
criteria.setFetchMode("primary", FetchMode.JOIN);
criteria.setFetchMode("history", FetchMode.JOIN);
And the (wrong) generated SQL:
select this_.primary as primary78_1_, this_.primary_id as prim2_78_1_, primary2_.history_id as unique1_56_0_, ...history fields
from DB_CATALOG.dbo.view_latest_status_history this_
left outer join DB_CATALOG.dbo.history primary2_ on this_.primary_id=primary2_.primary_id
where this_.specChange in (?, ?...)
I might've mucked up a few things when editing out the specifics of our project's DB schema, but the point is the first field in the 'select' clause is wrong:
this_.primary (view_latest_status_history.primary) is not a field; the field should be called primary_id. I think this may have something to do with the #Id annotation on the primary field? Any ideas how to fix this? If I remove the #Id, I get an error telling me that the entity has no primary key.
Update:
I no longer map the view as a field using a join table notation (as suggested below). The annotations have been revised, as follows. This solution works correctly in HQL, and generates the expected schema when hbm2ddl is enabled, but I have not re-tested it using the criteria query.
#Entity
#Table(name = "view_latest_status_history")
#Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ViewLatestStatusHistoryRow implements Serializable {
private String id;
private Primary primary;
private History history;
/**
* #return Returns the history.
*/
#OneToOne(optional = true)
#JoinColumn(name = "history_id", nullable = true)
#AccessType("field")
public History getHistory() {
return history;
}
//equals() and hashCode() implementations are omitted
#Id
#Column(name = "primary_id", nullable = false)
#Override
#AccessType(value = "field")
public String getId() {
return id;
}
/**
* #return Returns the primary.
*/
#PrimaryKeyJoinColumn(name = "primary_id", referencedColumnName = "unique_id")
#OneToOne(optional = false)
#AccessType("field")
public Primary getPrimary() {
return primary;
}
}
It most certainly is due to #Id annotation - primary_id is NOT a primary key in this case. Nor can you realistically have #Id and #ManyToOne on the same property.
Let me ask you this - why are you mapping ViewLatestStatusHistoryRow as an entity to begin with? It's not like you ever going to persist it. Consider mapping your latest history entry directly (as read-only) on primary (as many-to-one) and using your view as join table.

Categories

Resources