I have one Parent class with ID field, and two Child classes. I have applied TABLE PER CLASS inheritance.
Some Rows in these two tables have the same identifiers. (The same Id)
When findAll() of the JpaRepository is called I have org.hibernate.PropertyAccessException
What I can to do to resolve the conflict? I can not to change IDs in these tables, because they have a lot of dependencies and foreign key constraints.
#Entity
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Parent implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#TableGenerator(name = "content_id_generator",
table = "generic_sequences",
pkColumnName = "sequence_name",
valueColumnName = "sequence_value",
pkColumnValue= "content_seq",
allocationSize = 20)
#GeneratedValue(strategy = GenerationType.TABLE, generator = "content_id_generator")
#Column(updatable = false)
private Long id;
#Column(length = 250, nullable = false)
#NotNull
#Size(min = 1, max = 250)
private String title;
...
There are tho child classes Child1 and Child2 extends Parent.
There is another inheritance that dependent from Content.
#Entity
#Table(name = "text_id_changed")
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name = "type")
public abstract class DependentCalss implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(updatable = false)
private Long id;
...
DependentCalss has two child classes DependentCalssChild1 and DependentCalssChild2.
#Entity
#DiscriminatorValue("DependentCalssChild1")
public class DependentCalssChild1 extends DependentCalss {
private static final long serialVersionUID = 1L;
#ManyToOne
#JoinColumn(name = "child_1_id")
private Child1 child1;
...
AND
#Entity
#DiscriminatorValue("DependentCalssChild2")
public class DependentCalssChild2 extends DependentCalss {
private static final long serialVersionUID = 1L;
#ManyToOne
#JoinColumn(name = "child_2_id")
private Child2 child2;
...
And jpaRepository
#Repository
public interface DependentCalssRepository extends JpaRepository<DependentCalss, Long> {
}
When I call findAll() of DependentCalssRepository I catch org.hibernate.PropertyAccessException that field can not be set etc.
It happens when Child1 and Child2 have the same ID in separate tables.
What can I do to resolve it? I can not change id in tables because of great number dependencies and constraints. Thanks!
Related
What's the correct way to create bidirectional 1to1 mapping using Embeddable annotation? This one throws error
"EmpId has no persistent id property: Emp.id"
#Entity
public class Per implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "per_id")
private Long id;
#OneToOne
#JoinColumn(name = "per_id", referencedColumnName = "emp_id")
private Emp emp;
}
#Embeddable
public class EmpId implements Serializable {
private static final long serialVersionUID = 1L;
#OneToOne(mappedBy = "emp")
private Per per;
}
#Entity
public class Emp implements Serializable {
#EmbeddedId
private EmpId id;
}
I'd like to operate entities like
per.getEmp();
emp.getId().getPer();
I have two classes and I want to have a one to many relation between them, for e.g.:
Home(id<int>, rooms<string>)
Vehicle(id<int>, home_id<int>, name<string>)
I need to have a relation between Home and Vehicle class using Home.id and vehicle.home_id.
Please suggest any example which I can use here for CURD operation to implement REST service.
I need to have a relation between Home and Vehicle class using Home.id
and vehicle.home_id.
Your entities should look like this :
Vehicle Entity
#Entity
#Table(name = "vehicle", catalog = "bd_name", schema = "schema_name")
#XmlRootElement
public class Vehicle implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "id")
private Integer id;
#Column(name = "name")
private String name;
#JoinColumn(name = "home_id", referencedColumnName = "id")
#ManyToOne
private Home homeId;
//constructor getter & setters
}
Home Entity
#Entity
#Table(name = "home", catalog = "bd_name", schema = "schema_name")
#XmlRootElement
public class Home implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "id")
private Integer id;
#Column(name = "room")
private Character room;
#OneToMany(mappedBy = "homeId")
private List<Vehicle> vehicleList;
//constructor getter & setters
}
-- The Facts....
In my project every class/table should have a couple (Site,Structure) in order to provide multisite deploy.
I provided an Installation class
#Entity
#Table(name="COM_INSTALLATION")
#IdClass(InstallationPK.class)
public class Installation implements Serializable{
private static final long serialVersionUID = 3601006283578715263L;
#Id
#Column(name = "site")
private String site;
#Id
#Column(name = "structure")
private String structure;
/* ... constructor, getters, setters ... */
}
and its PK, as
public class InstallationPK implements Serializable {
private static final long serialVersionUID = 1L;
private String site;
private String structure;
/* .. constructor, getters, setters ... */
}
then I have a MappedSuperclass named BaseEntity
#MappedSuperclass
public class BaseEntity
{
private Installation installation;
#OneToOne
#JoinColumns({
#JoinColumn(name = "site", referencedColumnName = "site"),
#JoinColumn(name = "structure", referencedColumnName = "structure")
})
public Installation getInstallation() {
return installation;
}
public void setInstallation(Installation installation) {
this.installation = installation;
}
}
quite easy so far... every #Entity and #Table annotated class extending BaseEntity contains a column SITE, a column STRUCTURE and a related foreign key.
-- The Problem....
It happens that with a class implementing org.springframework.security.core.userdetails.UserDetails and extending BaseEntity, Hibernate does not create the two columns but an INSTALLATION column with RAW type. That's the class
#Entity
#Table(name="USR_USER", uniqueConstraints =
#UniqueConstraint(columnNames = { "username" }))
#SequenceGenerator(name = "userId_seq_generator", sequenceName = "S_USER")
public class User extends BaseEntity implements UserDetails{
private static final long serialVersionUID = 8698408700004341649L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "userId_seq_generator")
private Long userId;
/* ... getter, setters, ... */
}
and that's the result
Any ideas? Thanks in advance
I don't have point reputation to comment so i'll put here my comments:
Why instalationPK field isn't in instalation.class??
You should put #EmbeddedId in the field instalationPK in Installation.class.
I don't if makes differences, but put joinCollums on the field in BaseEntity:
#OneToOne
#JoinColumns({
#JoinColumn(name = "site", referencedColumnName = "site"),
#JoinColumn(name = "structure", referencedColumnName = "structure")
})
private Installation installation;
I'm trying to create a social app service. I have user with confirmed or nonconfirmed relationships.
When I load UserA, the result look like belove.
"result":{
"idUser":"UserA",
"unconFriendships":[
{
"idUser":"UserB",
"unconFriendships":[
{
"idUser":"UserC",
"unconFriendships":[
...
While it has to be look like
"result":{
"idUser":"UserA",
"unconFriendships":[
{
"idUser":"UserB",
"unconFriendships":null //only one level have to fetched
....
I thought that this was because jackson json library, I debbuged the code. Before serialization, I inspected userA object and I saw that userA.unconFriendships.userB.unconFriendships was not null and with size bigger than 0.
Nearly it has been 12 hours, still couldn't solve the problem. Please help me to solve this. Thanks in advence.
Here is UserEntity.java
#Entity
#Table(name="aduser",uniqueConstraints=#UniqueConstraint(columnNames = {"idUser","nmEmail"}))
#JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="cdUser")
public class UserEntity extends BaseEntity {
protected static final long serialVersionUID = 8864033727886664353L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "admin_seq")
#SequenceGenerator(name = "admin_seq", sequenceName = "CDUSER_SEQUENCE", allocationSize = 1)
#Column(name="cdUser")
private long cdUser;
#OneToMany(mappedBy = "owner", targetEntity=Friendship.class)
#JsonProperty
protected Set<UnconfirmedFriendship> unconFriendships;
#OneToMany(mappedBy = "owner", targetEntity=Friendship.class)
#JsonProperty
protected Set<UnconfirmedFriendship> conFriendships;
...
Friendship.java
#Entity
#Table(name="aduserfriend")
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name = "verified")
#JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="friend_cduser",scope=UserEntity.class)
public abstract class Friendship extends BaseEntity{
protected static final long serialVersionUID = -670863816551430192L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "cdFriendship")
private long cdFriendship;
#ManyToOne
#JsonIgnore
#JoinColumn(name = "owner_cduser")
protected UserEntity owner;
#ManyToOne
#JoinColumn(name = "friend_cduser")
protected UserEntity friend;
#Column(name = "verified",insertable=false,updatable=false)
private boolean verified;
...
UnconfirmedFriendship.java and ConfirmedFriendship.java
#Entity
#DiscriminatorValue(value = "0")//this value is 1 for Confirmed relationship
public class UnconfirmedFriendship extends Friendship {
private static final long serialVersionUID = 57796452166904132L;
}
I am having two tables Parent and Child. The query I want to use in Hibernate Criteria
SELECT tcr.*
FROM case_reminders tcr
INNER JOIN case_reminder_opr tco ON tcr.case_id = tco.case_id
WHERE tcr.case_status = 'OPN'
AND tco.operator_id = 111;
I have written the criteria as
Criteria ctr = getSession().createCriteria(CaseReminderOpr.class).add(Restrictions.eq("pk.oprOperatorId", operatorId));
ctr.createCriteria("pk.crmCaseId", "CR", Criteria.INNER_JOIN).add(Restrictions.eq("CR.caseStatus", STATUS.OPEN.getValue()));
List<CaseReminderOpr> oprList = ctr.list();
tried with createAlias as well but I am getting error as
ORA-00904: "CR1_"."CASE_STATUS": invalid identifier
Classes of CaseReminders(Parent) and CaseReminderOpr(Child) as follows.
#Entity
#Table(name = "CASE_REMINDERS")
public class CaseReminders implements Serializable {
#Id
#Column(name = "CASE_ID")
private Long caseId;
#Column(name = "CASE_STATUS")
private String caseStatus;
}
#Entity
#Table(name="CASE_REMINDER_OPR")
public class CaseReminderOpr implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
private CaseReminderOprPK pk;
}
#Embeddable
public class CaseReminderOprPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
#ManyToOne
#JoinColumn(name = "CASE_ID")
private CaseReminders crmCaseId;
#Column(name="OPERATOR_ID")
private Long operatorId;
}
Please help me with the inner_join query, appreciate your help again.
The change would be as below then it works. I have realized this later.
Make the Joincolumn as insertable=false,updatable=false in main entity class.
#Entity
#Table(name="CASE_REMINDER_OPR")
public class CaseReminderOpr implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
private CaseReminderOprPK pk;
#ManyToOne
#JoinColumn(name = "CASE_ID", insertable=false, updatable=false)
private CaseReminders caseRem;
}
Now the query should work as expected.
Criteria ctr = getSession().createCriteria(CaseReminderOpr.class, "CRO").add(Restrictions.eq("pk.oprOperatorId", operatorId));
ctr.createCriteria("CRO.caseRem", "CR", Criteria.INNER_JOIN).add(Restrictions.eq("CR.caseStatus", STATUS.OPEN.getValue()));
List<CaseReminderOpr> oprList = ctr.list();
Hopefully I am clear in explaining.