Risks of #ID in #MappedSuperclass - java

Actually, i want to change the entity id from Type Long to the UUID.
All my entites have a MappedSuperclass. To save time, I wanted to add the new solution into the MappedSuperclass object and remove the old solution from the specific entities.
Is this a good practice or is it associated with risks?
old solution
#Entity
public class CustomObject extends DomainObject {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID")
private Long id;
}
new solution
#MappedSuperclass
public class DomainObject {
#Id
#GenericGenerator(name = "uuid", strategy = "uuid2")
#GeneratedValue(generator = "uuid")
#Type(type="pg-uuid")
#Column(name = "ID")
private UUID id;
}

Related

UUID does not have hyphens in between

Inspired by the suggestion given here - JPA Entity class giving error with 2 #GeneratedValue fields, my question is the OPPOSITE of this - How to generate uuids without dashes
I am using H2 DB and have this in my model:
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
#Column(name = "useruuid")
#GeneratedValue(generator = "uuid")
#GenericGenerator(name = "uuid", strategy = "uuid4")
private UUID userUUID;
And in my controller it goes like this:
#PostConstruct
private void postConstruct() {
AppUser appUser = new AppUser(1l, UUID.randomUUID());
appUserJPARepository.save(appUser);
}
Now when my Spring Boot app starts my H2 DB-Console shows this:
ID USERUUID
1 25b9b7f391d94825b349866fe9a9077c
Question: How do I get hyphens in the DB? So that my uuid in the DB will be - 25b9b7f3-91d9-4825-b349-866fe9a9077c
I fiddled with #GenericGenerator(name = "uuid4", strategy = "uuid4") uuid(1) to uuid5 but got the same result. What is going on here and what I am doing wrong or what should I do to get hyphens in the DB? Any help or related info/links relevant to this will be greatly appreciated.
You can just remove
#GeneratedValue(generator = "uuid")
#GenericGenerator(name = "uuid", strategy = "uuid4")
and the code will looks like
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
#Column(name = "useruuid")
#GeneratedValue
private UUID userUUID;

How to maintain foreign key relationship in hibernate

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
}

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().

JPA modelling, one-to-one relation?

I am new to JPA and stuggles with defining the relations between my classes. I have a class called Player and a class called Game. A game holds references to two Player instances. The question is, how should this be modelled?
This is my current code:
#Entity
#Table(name = "t_player")
#JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Player {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Basic
#Column(name = "name")
private String name;
#Basic
#Column(name = "uuid")
private final String uuid = UUID.randomUUID().toString();
I think this is ok, but my problem is in the Game class:
#Entity
#Table(name = "t_game")
#JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Game {
public Game() {
}
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Basic
#Column(name = "uuid")
private final String uuid = UUID.randomUUID().toString();
#OneToOne
#PrimaryKeyJoinColumn
#JoinColumn(name = "id")
private Player player_1;
#OneToOne
#PrimaryKeyJoinColumn
#JoinColumn(name = "player_2")
private Player player_2;
public Game(Player player_1, Player player_2) {
this.player_1 = player_1;
this.player_2 = player_2;
}
}
This is not working, my table t_game only has two field; id and uuid. Where is my problem?
Remove the PrimaryKeyJoinColumn annotation, as I don't think it is what you meant to use, as it conflicts with the joincolumn definition. Use the joincolumn annotation instead to define the foreign key field name and the field it references if necessary.

Categories