[products_products_id])) must have same number of columns as the referenced primary key (products [categories_id,products_products_id]) - spring

i have error related to the hibernate query
Foreign key (FK_jipqfc4qahmxslbxjbln9cxpm:products [products_products_id])) must have same number of columns as the referenced primary key (products [categories_id,products_products_id])
i dont understand why it is i try lots of time but till now problem does not solve
Here is my oneToMany relation on categories pojo
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column
private int categories_id;
#Column
private String categories_image;
#Column
private int sort_order;
#Column
#Temporal(javax.persistence.TemporalType.DATE)
private Date date_added;
#Column
private Date last_modified;
#Column
private String categories_name;
public String getCategories_name() {
return categories_name;
}
public void setCategories_name(String categories_name) {
this.categories_name = categories_name;
}
#OneToOne(cascade = CascadeType.ALL, fetch= FetchType.LAZY)
#JoinTable(name="categories_description",joinColumns={
#JoinColumn(name = "categories_id",referencedColumnName="categories_id")})
private categories_description categories_description;
#OneToMany(cascade = CascadeType.ALL, fetch= FetchType.LAZY)
#JoinTable(name="products_to_categories",joinColumns={
#JoinColumn(name = "categories_id", referencedColumnName="categories_id")})
private Set<products_to_categories> products_to_categories;
#OneToMany(cascade = CascadeType.ALL, fetch= FetchType.LAZY)
#JoinTable(name="products",joinColumns={
#JoinColumn(name = "categories_id",referencedColumnName="categories_id")})
private Set<products> products;
and here is my products pojo
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column
private int products_id;
#Column
private int Category_id;
#Column
private int products_quantity;
#Column
private String products_model;
#Column
private String products_image;
#Column
private float products_price;
#Column
private Date products_date_added;
#Column
private Date products_last_modified;
#Column
private Date products_date_available;
#Column
private float products_weight;
#Column
private String products_status;
#Column
private int products_tax_class_id;
#Column
private int manufacturers_id;
#Column
private String products_type;
#OneToOne
#JoinColumn(name="products_id")
private products_description products_description;
#OneToOne
#JoinColumn(name="products_id")
private products_to_categories products_to_categories;
i want categories_id as a foregin key and also primary key inside products table
but while excuting i got this error .
please help i really appreciated.

Related

Hibernate Enver - Listening for only one change in referenced class

I have an entity as shown below that I am auditing using Hibernate Enver
#Entity
#Table(name = "watch_item")
#Audited
public class WatchItemEntity implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Type(type = "uuid-char")
#Column(name = "watch_item_id", columnDefinition = "VARCHAR(36)")
private UUID watchItemId;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "watch_model_id")
private WatchModelEntity watchModel;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "private_user_id")
private PrivateUserEntity privateUser;
private String serialNumber;
private Integer productionYear;
private String generalCondition;
private Boolean isMovementFullyFunctional;
private Boolean isInOriginalCondition;
private String comment;
private Boolean isProofOfPurchaseAvailable;
private String country;
private Boolean isCustomsDeclared;
private Boolean hasPaper;
private Boolean hasBox;
private String otherAccessories;
#CreatedDate private LocalDateTime createdDate;
#LastModifiedDate private LocalDateTime modifiedDate;
private String lastServiceProvider;
private LocalDate lastServiceDate;
private BigDecimal lastServiceCost;
private BigDecimal purchasedPrice;
private LocalDate purchasedOn;
...
}
As you can see, it has a PrivateUserEntity field. I want Hibernate Envers to record a change when the privateUser changes (and not record changes in PrivateUserEntity that correspond to the privateUser). However, I don't want to create a Private_User_Aud table. To give some context, a WatchItem can only be owned by one PrivateUser and hence, when the PrivateUser field changes, that means that the WatchItem's owner changed. The entity can be seen below
#Entity
#Table(name = "private_user")
public class PrivateUserEntity implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Type(type = "uuid-char")
#Column(name = "private_user_id", columnDefinition = "VARCHAR(36)")
private UUID privateUserId;
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "user_id")
private UserEntity user;
#LastModifiedDate private LocalDate modifiedDate;
private String title;
private String firstName;
private String lastName;
private String phone;
private LocalDate dateOfBirth;
private String email;
private String gender;
private String nationality;
private String residencyPermitType;
private LocalDate residencyPermitValidSince;
private String preferredLanguage;
...
}
Is this possible? And if so, how?
You can disable audit for it, doesn't work?
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "private_user_id")
#NotAudited
private PrivateUserEntity privateUser;

SQL Joining tables Using Spring JPA

I have some trouble with joining tables using Spring Boot JPA
I need to do this kind of joining:
**book2user — book to user
id INT NOT NULL AUTO_INCREMENT
time DATETIME NOT NULL
type_id INT NOT NULL
book_id INT NOT NULL
user_id INT NOT NULL**
Here are my Entity classes:
#Entity
#Table(name = "user")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String hash;
private Date reg_time;
private Integer balance = 0;
private String name;
// getters and setters
public class Book {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#ManyToOne
#JoinColumn(name = "author_id", referencedColumnName = "id")
private Author author;
#ManyToOne
#JoinColumn(name = "genre_id", referencedColumnName = "id")
private Genre genre;
private String title;
private String slug;
private String description;
private String priceOld;
private String price;
private String image;
private boolean is_bestseller;
private Date pub_date;
// getters and setters
In order to do this kind of joining I should create another entity class ??
Something like this should work:
#Entity
#Table("book2user")
public class Book2User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private LocalDateTime time;
private Integer typeId;
#OneToOne
private Book book;
#OneToOne
#JoinColumn(name = "user_id") //optional, if the name of the table and field matches.
private User user;
}

Jpa-Jpql Query to Select All field from parent Entity and specific field from child Entity in OneToOneMapping

I have oneToOne RelationShip between Employee and student entity i want to fetch all field from Employee Entity and name and modelNumber From Laptop Entity
Employee Entity
public class Employee {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "employee_name")
private String name;
#Column(name = "date_of_birth")
private String dob;
#Column(name = "gender")
private char gender;
#Column(name = "skills")
private String[] skills;
#OneToOne(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
#JoinColumn(name = "laptop_id")
private Laptop laptop;
//getter setter
Laptop Entity
#Entity
public class Laptop {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String ram;
private String modelNumber;
private String processor;
//getter Setter
select e.id, e.name, e.dob, e.gender, e.skills,
e.laptop.name, e.laptop.modelNumber
from Employee e
Please start by reading the docs: https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#hql

I am not getting unique column using hibernate , for postgresql db

#Entity
#Table(uniqueConstraints={#UniqueConstraint(columnNames={"catgoryId","applcationNo"})})
I tried this explicitly #table
and unique , //but not getting result.
public class DmsDocDetailPojo {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(columnDefinition = "serial")
private Long dmsDocId;
#Column
private String stateCode="AI";
#Column(name = "applicationNo", unique = true,nullable=false)
#NotNull
private String applcationNo;
#Column(name = "catgoryId", unique = true,nullable=false)
private String catgoryId;
#CreationTimestamp
#Column( nullable = false, updatable=false)
private Date doc_uploaded_dt;
#UpdateTimestamp
private Date doc_updated_dt;
#Column(name = "document_file", columnDefinition = "BYTEA")
private byte[] document_file;
#Column
private String fileName;
#Column
private Integer fileSize;
}
check your database primary key it should be an auto increment

springboot jpa combine two tables

I want to query data from two tables,
location field in Translation is a foreign key from id field of Location
#Entity
#Table(name = "Translation")
#Data
public class Translation {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#NotNull private String language;
#NotNull private String name;
#NotNull private String description;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "location", nullable = false, insertable = false, updatable = false)
#Fetch(FetchMode.JOIN)
private Location location;
}
#Entity
#Table(name = "Location")
#Data
public class Location {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#NotNull private String code;
#NotNull private String type;
private Double longitude;
private Double latitude;
#Column(name = "parent_id")
private Integer parentId;
#OneToMany(targetEntity = Translation.class, mappedBy="id", fetch = FetchType.EAGER)
private Set<Translation> translations;
}
————————————————————————————————————————
But when I use a query
#Query(
"SELECT new com.afkl.travel.exercise.model.RetrieveLocationResponse("
+ "loc.code, tran.name, loc.type, loc.latitude, loc.longitude, tran.description, loc.parentId)"
+ "FROM Location loc LEFT JOIN loc.translation tran")
List<RetrieveLocationResponse> fetchLeftJoin();
All the fields related to Translation is null, having no idea what happened
UPDATE
The following ones work for me.
#OneToMany(mappedBy = "location", cascade = CascadeType.ALL)
#JsonIgnore
private Set<Translation> translations;
#ManyToOne
#JoinColumn(name = "location")
private Location location;
try
#OneToMany(mappedBy = "location", cascade = CascadeType.ALL)
#JsonIgnore
private Set<Translation> translations;
#ManyToOne
#JoinColumn(name = "location")
private Location location;

Resources