#GeneratedValue - how do I replace it with my function? (Java 8) - java

Now the generator goes sequentially with step 1.
I need the ID to generate its own function.
How to substitute its function?
Thank you.
#Entity
#Table(name = "MovementHistory")
public class MovementHistory {
#Id
#SequenceGenerator(name="SEQ_GEN_MH", sequenceName="SEQ_MH", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator="SEQ_GEN_MH")
private long id;
private long waybillID;
private Date dateEvent;
#ManyToOne(fetch = FetchType.EAGER)
private Status status;
}

Related

Create entity using join in jpa hibernate

I'm stuck with a problem in Java, hibernate (jpa)
So, I have 2 classes: Class and Classroom, each one being entities
#Entity
#Table(name = "CLASSES")
public class Class {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="ID")
private Long id;
#Column(name = "TEACHER_ID")
private Long teacherId;
#Column(name = "NAME")
private String name;
#Column(name = "YEAR")
private Integer year;
#Column(name = "SECTION")
private String section;
}
#Entity
#Table(name = "CLASSROOMS")
public class Classroom {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID")
private Long id;
#Column(name = "NAME")
private String name;
#Column(name = "LOCATION")
private String location;
#Column(name = "CAPACITY")
private Integer capacity;
}
Also, I have another java class called Planner which connect these two classes (their tables - using classroom_id and class_id); I have a table for this Planner
#Entity
#Table(name = "PLANNERS")
public class Planner {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID")
private Long id;
#Column(name = "CLASSROOM_ID")
private Long classroomId;
#Column(name = "CLASS_ID")
private Long classId;
#Column(name = "STARTTIME")
private Time startTime;
#Column(name = "ENDTIME")
private Time endTime;
#Column(name = "DATA")
private Date date;
}
What I need: a new entity (or just output data) which will include all fields from PLANNERS, field NAME from CLASSES and field NAME from CLASSROOMS.
In SQL, this query is:
select M.classroom_id, M.class_id, M.starttime, M.endtime, M.data, CL.NAME AS "ROOM NAME (FROM CLASSROOMS)", C.NAME AS "COURSE NAME (FROM CLASSES)" FROM PLANNERS M INNER JOIN CLASSROOMS CL ON CL.ID = M.CLASSROOM_ID INNER JOIN CLASSES C ON C.ID = M.CLASS_ID
(inner join using classroom_id and class_id)
How can I do this on hibernate jpa? I want to get the objects (rows) returned by the above query.
I searched a lot, I find about join column, other annotations (e.g. OneToMany etc) but I didn't succeed, so I need help

One table has two same joins in another table

I have one table called image and another table called duplicate. There are two OneToMany relations are associated.
I am not quite sure whether below implementation is the right approach for that. Moreover whether we require that second private List<DuplicateEntity> duplicateEntities2; ?
ImageEntity:
#Entity
#Table(name = "image")
public class ImageEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "filename")
private String fileName;
#OneToMany(mappedBy = "imageEntity1")
private List<DuplicateEntity> duplicateEntities1;
#OneToMany(mappedBy = "imageEntity2")
private List<DuplicateEntity> duplicateEntities2;
}
DuplicateEntity:
#Entity
#Table(name = "duplicate")
public class DuplicateEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#ManyToOne
#JoinColumn(name = "image_a_id")
private ImageEntity imageEntity1;
#ManyToOne
#JoinColumn(name = "image_b_id")
private ImageEntity imageEntity2;
}

Hibernate/JPA incorrect join in Spring Boot Application

See code below for my 2 entity classes - when I call the findAll() method from my OrigRepository class, it joins these two tables using both primary keys. I want the join to be between the primary key of the Orig table and the foreign key entry in the MsgResponse table ("OrigID") - any sugggestions?
Orig Entity
#Entity
#Table(name = "originator")
public class Orig {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "OrigID")
private int OrigID;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "OrigID")
private MsgResponse responseInfo;
}
MsgResponse Entity
#Entity
#Table(name = "message_response")
public class MsgResponse {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private int responseId;
#Column(name = "OrigID")
private int OrigId;
#OneToOne(mappedBy="responseInfo")
private Orig OrigInfo;
}
I suggest you to see the jpa documentation
here.
Example 1 should be your case
Try to swap relation ownership, that is:
#Entity
#Table(name = "originator")
public class Orig {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "OrigID")
private int OrigID;
#OneToOne(mappedBy="origInfo")
private MsgResponse responseInfo;
}
#Entity
#Table(name = "message_response")
public class MsgResponse {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private int responseId;
// #Column(name = "OrigID")
// private int origId;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "OrigID")
private Orig origInfo;
}
Note that the #JoinColum annotation is in now in the MsgResponse entity. This is because in a #OneToOne the join column refers to the source entity (see here).
Hope this could help.

Hibernate not fetching subentity if distinct not present

We have the following domain:
Members and Channels.
Member - equivalent of a user.
#Entity
#Table(name = "member")
public class Member {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "first_name")
private String firstName;
}
Channel - Will group blog posts by topic.
#Entity
#Table(name = "channel")
public class Channel {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
#Column(name = "name")
private String name;
}
The members can be assigned to channels thus becoming members in the channel. So we have the ChannelMember entity since we don't always want to retrieve both ends of the relationship.
#Entity
#Table(name = "channel_member")
public class ChannelMember {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#OneToOne
#JoinColumn(name = "channel_id", nullable = false)
private Channel channel;
#OneToOne
#JoinColumn(name = "member_id", nullable = false)
private Member member;
}
We use Spring Data JPA and MySQL for our persistance(Spring Boot 1.5.3 Release).
Why is the distinct mandatory in the JPQL query for Hibernate to be able to fetch any records in the following case?
Repository code example as follows:
#Repository
public interface MemberRepository extends JpaRepository<Member, Long> {
#Query("select cm.member from ChannelMember cm")
List<Member> getNotWorking(); // empty List will be returned
#Query("select distinct cm.member from ChannelMember cm")
List<Member> getWorking();
}

although true entity naming i get ( mappedBy reference an unknown target entity property exception)

although true entity naming i get mappedBy reference an unknown target entity property
regarding to this and this the solution for mappedby exception is to naming the entity correctly i did that but i still getting the exception
#Entity
#Table(name = "CONTEST")
public class Contest extends eg.com.etisalat.base.entity.BaseEntity implements
Serializable {
private static final long serialVersionUID = 1L;
#Id
#SequenceGenerator(name = "CONTEST_ID_GENERATOR", sequenceName = "SEQ_CONTEST_ID")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CONTEST_ID_GENERATOR")
#Column(name = "ID")
private long contestId;
#OneToMany(cascade = { CascadeType.REMOVE, CascadeType.REFRESH }
,mappedBy="contest")
private List<Challenge> challenges;
//attributes
//getters &setters
}
and this is the ohter entity which has a many to one relation with contest entity
#Id
#SequenceGenerator(name = "CHALLENGE_ID_GENERATOR", sequenceName = "SEQ_Challenge_ID")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CHALLENGE_ID_GENERATOR")
#Column(name = "CHALLENGE_ID")
private long Id;
#ManyToOne(targetEntity=Contest.class)
#JoinColumn(name = "CONTEST_ID")
private Contest conestId;
//attributes
//getters &setters
}
thanks in advance
In Challenge entity, the field should be private Contest contest; with a matching getter and setter - getContest() and setContest().

Categories