Unique together validation - spring

#Entity
#NoArgsConstructor
#AllArgsConstructor
#Builder
#Table(name = "overoptimisation_identifiers")
public class OveroptimisationIdentifierEntity {
#Id
#GeneratedValue(generator = "uuid2")
#GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
#Column(name = "id", columnDefinition = "VARCHAR(255)")
private UUID id;
#CreationTimestamp
private LocalDate date;
}
I'd like to have only one identifier per day. This means that I have to organise a constraint: id and date are unique together.
By the way, maybe it is not done by JPA. It may be done by the framework, a third party library or just by me in the code. But I don't know how to do that.

Related

Multiple list of Data comming in API when using ManyToOne Relationship

CarMake.java
#Getter
#Setter
#EqualsAndHashCode
#NoArgsConstructor
#Entity
public class CarMake {
#SequenceGenerator(
name = "car_make_sequence",
sequenceName = "car_make_sequence",
allocationSize = 1
)
#Id
#GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "car_make_sequence"
)
private Long id;
private String name;
#CreationTimestamp
private LocalDateTime createdAt;
#UpdateTimestamp
private LocalDateTime updatedAt;
#OneToMany(mappedBy = "make", cascade = CascadeType.ALL)
private List<CarModel> model;
}
CarModel.java
#Getter
#Setter
#EqualsAndHashCode
#NoArgsConstructor
#Entity
public class CarModel {
#SequenceGenerator(
name = "car_model_sequence",
sequenceName = "car_model_sequence",
allocationSize = 1
)
#Id
#GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "car_model_sequence"
)
private Long id;
private String name;
private Integer beginDate;
private Integer endDate;
#CreationTimestamp
private LocalDateTime createdAt;
#UpdateTimestamp
private LocalDateTime updatedAt;
#ManyToOne(fetch = FetchType.EAGER)
CarMake make;
#OneToMany(mappedBy = "model", cascade = CascadeType.ALL)
private List<CarVariant> variants;
}
CarVariant.java
#Getter
#Setter
#EqualsAndHashCode
#NoArgsConstructor
#Entity
public class CarVariant {
#SequenceGenerator(
name = "car_variant_sequence",
sequenceName = "car_variant_sequence",
allocationSize = 1
)
#Id
#GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "car_variant_sequence"
)
private Long id;
#Column(nullable = false)
private String name;
#CreationTimestamp
private LocalDateTime createdAt;
#UpdateTimestamp
private LocalDateTime updatedAt;
#ManyToOne(fetch = FetchType.LAZY)
CarModel model;
}
If i am using repository i am getting loop of data which is not present even in database
#GetMapping("api/car/model/{id}")
#ResponseBody
public Iterable<CarMake> showCarModelsByMakeId(#PathVariable Long id){
return carMakeRepository.findAll();
}
Bellow is the response of API
[{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983","updatedAt":"2022-05-06T17:07:48.40903","model":[{"id":1,"name":"hello","beginDate":1,"endDate":3,"createdAt":"2022-05-06T17:07:59.440783","updatedAt":"2022-05-06T17:07:59.440826","make":{"id":1,"name":"hello","createdAt":"2022-05-06T17:07:48.408983",
I beleive i should get only one instance of data as only one row is present in table. what is the reason i am getting so much data, did i created onetomany relationship correctly?, how i can achieve it
Database data
This is a famous bidirectional issue. In few words, your CarMake entity references CarModel, which in its turn, references back to CarMake, what causes an infinitive loop.
Try to put #JsonIgnore annotation on CarMake make in your CarModel entity and repeat the test.
PS: better to not to return entity objects from your rest controller, use either DTO mapping, or projections
PPS: don't use #EqualsAndHashCode on #Entity classes

How do I map an #OneToMany and #ManyToOne relationship properly so that I can save and update the #OneToMany side with or without the #ManyToOne side

I have an app with Angular front end and Spring backend. The two classes in question here are (backend):
#Setter
#Getter
#AllArgsConstructor
#NoArgsConstructor
#Entity
#Table(name = "tournament_games")
#JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class TournamentGame {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
#ManyToOne(fetch = FetchType.EAGER, optional = false)
#JoinColumn(name = "code", foreignKey = #ForeignKey(name = "code_fk"))
private TournamentCode code;
#ManyToOne(fetch = FetchType.EAGER, optional = false)
#JoinColumn(name = "type", foreignKey = #ForeignKey(name = "game_type_fk"))
private GameType type;
#Column(name = "home_score")
private int home_score;
#Column(name = "away_score")
private int away_score;
#ManyToOne(fetch = FetchType.EAGER, optional = false)
#JoinColumn(name = "result_type", foreignKey = #ForeignKey(name = "result_type_fk"))
private ResultType result_type;
#Column(name = "status")
private boolean status;
#Column(name = "round")
private int round;
#Column(name = "locked")
private boolean locked;
#OneToMany(mappedBy = "game", fetch = FetchType.EAGER)
private List<TournamentGamesPlayers> players = new ArrayList<>();
}
and
#Setter
#Getter
#AllArgsConstructor
#NoArgsConstructor
#Entity
#Table(name = "tournament_games_players")
#JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "game")
public class TournamentGamesPlayers implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#ManyToOne
#JoinColumn(name = "tournament_game_id")
private TournamentGame game;
#Id
#ManyToOne
#JoinColumn(name = "playerid")
private Player player;
#Column(name = "home")
private boolean home;
}
I need help figuring out how to persist the List<TournamentGamesPlayers> when I save and/or update a TournamentGame object. I generate 45 games. The first 30 games have known players, and so I set them before saving. The last 15 do not have entries for the TournamentGamesPlayers join table, because I need to add them later.
I am able to get some results with CascadeType.ALL on the #OneToMany side when I initially generate the games, but it fails when I try to update a game with a seemingly infinite recursion/stack overflow.
If I omit any cascade type, the games side get generated, but the join table #ManyToOne side does not get entered.
I ended up just putting the players back into the game table to make my life easier.
try putting CascadeType.MERGE, CascadeType.ALL "delete parent and orphans" (JPA CascadeType.ALL does not delete orphans).
Also, defining the relationship as EAGER and not ignoring the JSON property can have problems. I would add #JsonIgnore to one of the parts of the relationship

How to make composite Foreign Key part of composite Primary Key with Spring Boot - JPA

I have a problem with the historization of objects in the database.
the expected behavior of the save JpaRepository method is : Insert in the two tables idt_h and abo_h
But the current behavior is Insert in the idt_h table and update in the abo_h table.
#Data
#Entity
#Table(name = "ABO_H")
#AllArgsConstructor
#NoArgsConstructor
public class AboOP {
#Id
#Column(name = "ABO_ID")
private String id;
#Column(name = "ABO_STATUT")
private String statut;
#Column(name = "ABO_DATE_STATUT")
private Instant date;
#Column(name = "ABO_CoDE")
private String code;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumns({
#JoinColumn(name = "IDC_ID", referencedColumnName = "IDC_ID"),
#JoinColumn(name = "DATE_HISTO", referencedColumnName = "DATE_HISTO")
})
private IdtOP idtOP;
}
#Data
#Entity
#Table(name = "IDT_H")
#AllArgsConstructor
#NoArgsConstructor
public class IdtOP {
#AttributeOverrides({
#AttributeOverride(name = "id",
column = #Column(name = "IDC_ID")),
#AttributeOverride(name = "dateHisto",
column = #Column(name = "DATE_HISTO"))
})
#EmbeddedId
private IdGenerique idtId = new IdGenerique();
//Other fields
}
#Data
#AllArgsConstructor
#NoArgsConstructor
#Embeddable
public class IdGenerique implements Serializable {
private String id;
private Instant dateHisto;
}
I think that the class IdGenerique which groups the id and dateHisto is not well invoked for the table abo_h ??
thanks in advance
When you use the save() method, entityManager checks if the entity is new or not. If yes, the entity will be saved, if not, it'll be merged
If you implement your Entity Class with the inteface Persistable, you can override the method isNew() and make it returns True. In that case the save() method will persist, and not merge, your entity.

Inner join on two tables in spring boot

I have 2 entities and want to perform an inner join on the ID of these two tables. How do I do that? After joining the tables, how do I get the values?
First entity: Employee.java
#Entity
#Table(name = "emp")
public class Employee {
#Id
#Column(name = "id", nullable = false)
private int id;
#Column(name = "language", nullable = false)
private String language;
Second entity: Username.java
#Entity
#Table(name = "users")
public class Username {
#Id
#Column(name = "id", nullable = false)
private int id;
#Column(name = "name", nullable = false)
private String name;
Thanks
I don't know it's helpful for your or not but,
You have to give relationship between those table first(Here i defined bidirectional relationship).
I suppose there is #OneToOne mapping. As like follow,
In Employee Table,
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "username_id")
private Username username;
#OneToOne(mappedBy = "employee")
private Employee employee;
Same way whenever you need those data base on requirement then Place Query as following way in your Employee Repository,
#Query(nativeQuery = true, value="<your-join-query>")
public Employee getEmployeeAllDetails();
For more brief detail follow this kind of tutorials which give you better idea regurding working mechenisum.
https://howtodoinjava.com/
https://www.baeldung.com/

Spring Framework + Spring Data + Hibernate Jpa OneToMany child removal fails

I have an unidirectional OneToMany JPA entity mapping in my (Spring Framework + Spring Data + Hibernate JPA) project. Entity classes are like in the following code.(I have removed irrelevant class members for brevity).
#Entity
#Table(name = "employees")
class Employee{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Integer id;
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
#JoinColumn(name = "employee_id")
private List<DepartmentAssignment> departmentAssignments = new ArrayList<>();
}
#Entity
#Table(name = "department_assignments")
class DepartmentAssignment{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Integer id;
#NotNull
#Column(name = "employee_id")
private Integer employeeId;
#NotNull
#Column(name = "department_id")
private Integer departmentId;
}
#Entity
#Table(name = "departments")
class Department{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Integer id;
}
And, in one of my service classes have a method to remove a DepartmentAssignment from an Employee like below.
public Employee deleteDepartmentAssignment(Integer empId, Integer deptAssignmentId) {
Employee employee = employeeRepository.findOne(empId);
if(employee != null) {
for ( DepartmentAssignment da : employee.getDepartmentAssignments()) {
if(da.getId().equals(deptAssignmentId)) {
employee.getDepartmentAssignments().remove(da);
employee = employeeRepository.save(employee);
break;
}
}
}
return employee;
}
However, calling above methods gives me an error: org.hibernate.exception.ConstraintViolationException ,and in the SQL log, I can see Column 'employee_id' cannot be null error for the last SQL statement of the transaction.
Can anybody tell me what I'm doing wrong here and how to get it fixed?
You don't need to add
#NotNull
#Column(name = "employee_id")
private Integer employeeId;
to the Employee, if you use #JoinColumn(name = "employee_id"). Try to remove it.
You can try the following, not sure why you use the plain id in the object. Thats not object relational mapping.
For more details see Hibernate triggering constraint violations using orphanRemoval
#Entity
#Table(name = "employees")
class Employee{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "employee", orphanRemoval = true)
private List<DepartmentAssignment> departmentAssignments = new ArrayList<>();
}
#Entity
#Table(name = "department_assignments")
class DepartmentAssignment{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#ManyToOne(optional=false)
private Employee employee;
#ManyToOne(optional=false)
private Department department;
}
#Entity
#Table(name = "departments")
class Department{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
}
You must look .hbm.xml file and you should mapping your Entity in this file and
you can look this example
http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example/
I hope it will be useful for you.
try removing
cascade = CascadeType.ALL
but im not 100% sure..

Resources