I have been stuck with one scenario in hibernate, i tried looking for such scenario discussion but i couldnt get so raing here for any suggestions or help,
i have three tables category , category_artical, artical.
CREATE TABLE `category` (
`category_id` int(11) NOT NULL AUTO_INCREMENT,
`category_name` varchar(45) NOT NULL,
`category_description` varchar(45) NOT NULL,
PRIMARY KEY (`category_id`)
);
CREATE TABLE `article` (
`article_id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(70) NOT NULL,
`description` varchar(250) NOT NULL,
`keywords` varchar(150) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`article_id`)
);
CREATE TABLE `category_article` (
`category_id` int(11) NOT NULL,
`article_id` int(11) NOT NULL,
'category_article_status' varchar(1) NOT NULL.
'category_article_type' varchar(2) NOT NULL,
PRIMARY KEY (`category_id`,`article_id`),
UNIQUE KEY `article_id_UNIQUE` (`article_id`),
KEY `fk_category` (`category_id`),
KEY `fk_article` (`article_id`),
CONSTRAINT `fk_article` FOREIGN KEY (`article_id`) REFERENCES `article` (`article_id`),
CONSTRAINT `fk_category` FOREIGN KEY (`category_id`) REFERENCES `category` (`category_id`)
);
i have entity for category,
#Entity
#Table(name = "CATEGORY")
public class Category {
private long id;
private String name;
#Id
#GeneratedValue
#Column(name = "CATEGORY_ID")
public long getId() {
return id;
}
#OneToMany(cascade = CascadeType.ALL)
#JoinTable(
name = "CATEGORY_ARTICLE",`enter code here`
joinColumns = #JoinColumn(name = "CATEGORY_ID"),
inverseJoinColumns = #JoinColumn(name = "ARTICLE_ID")
)
private Set<Article> articles;
// other getters and setters...
/*For Article entity is */
#Entity
#Table(name = "ARTICLE")
public class Article {
private long id;
private String title;
private String description;
private String keywords;
private String content;
#Id
#GeneratedValue
#Column(name = "ARTICLE_ID")
public long getId() {
return id;
}
// other getters and setters...
For category_article i have no entity as i am using #jointable in category
So everything work fine till i have only two column in category_article which are category_id and article_id. But as soon as i added two more columns i am confused how to inser data in those coulmns(category_article_status and category_article_type) which are not null.
any suggestions on this.
You should create another entity for CATEGORY_ARTICLE like this.
#Entity
#Table(name = "CATEGORY_ARTICLE")
public class CategoryArticle{
#Id
private Long id;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "category_id")
private Category category;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "article_id")
private Article article;
// other columns
private Boolean categoryArticleStatus;
private Long categoryArticleType;
}
and also should change Category and Article. Remove JoinTable from category entity and change it as bellow.
Category
#Entity
#Table(name = "CATEGORY")
public class Category {
//.....
#OneToMany(mappedBy = "category")
private Set<CategoryArticle> categoryArticleSet;
}
Article
#Entity
#Table(name = "ARTICLE")
public class Article {
//.....
#OneToMany(mappedBy = "article")
private Set<CategoryArticle> categoryArticleSet;
}
Related
I am creating entity relationships in Spring Boot data JPA. Since those tables being legacy I am not able to modify or add columns. Issue is I am getting error if point part of embedded Id.
My entity classes looks like below:
Class Customer {
#EmbededId
private CustomerPk id;
#Column("NAME")
private String name;
#OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="customerDetails")
private List<Purchase> purchaseDetails;
...
}
#Embeddable
Class CustomerPk {
#Column("CUSTOMER_ID")
private String customerId
#Column("PURCHASE_ID")
private String productId;
#Column("PURCHASE_DATE")
private String date;
}
Purchase Entity looks like below:
Class Purchase {
#EmbededId
private PurchasePK id;
#Column("TRANSACTION_NAME")
private String transactionName;
#ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
#JoinColumns({
#JoinColumn(name="CUSTOMER_ID" referencedColumnName="CUSTOMER_ID")
#JoinColumn(name="PURCHASE_ID" referencedColumnName="PURCHASE_ID")
)}
private Customer customerDetails;
...
}
#Embeddable
Class PurchasePK {
#Column("CUSTOMER_ID")
private String customerId
#Column("PURCHASE_ID")
private String productId;
#Column("TRANSACTION_DATE")
private String date;
}
With above structure I am getting org.hibernate.AnnotationException: referencedColumnNames(CUSTOMER_ID, PURCHASE_ID) of Purchase.customerDetails referencing Customer not mapped to a single property.
If I remove date property from CustomerPK, I am able to make the server up. But with current requirement I need date to be part of the CustomerPK class.
I think if I use part of the composite key as Join Columns I am getting this error.
Working version:
#Entity
public class Customer {
#EmbeddedId
private CustomerPk id;
#Column(name = "NAME")
private String name;
#OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "customerDetails")
private List<Purchase> purchaseDetails;
}
#Embeddable
public class CustomerPk implements Serializable {
#Column(name = "CUSTOMER_ID")
private String customerId;
#Column(name = "PURCHASE_ID")
private String productId;
#Column(name = "PURCHASE_DATE")
private String date;
}
#Entity
public class Purchase {
#EmbeddedId
private PurchasePK id;
#Column(name = "TRANSACTION_NAME")
private String transactionName;
#ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinColumns({
#JoinColumn(name = "CUSTOMER_ID", referencedColumnName = "CUSTOMER_ID", insertable = false, updatable = false),
#JoinColumn(name = "PURCHASE_ID", referencedColumnName = "PURCHASE_ID", insertable = false, updatable = false),
#JoinColumn(name = "PURCHASE_DATE", referencedColumnName = "PURCHASE_DATE", insertable = false, updatable = false)
})
private Customer customerDetails;
}
#Embeddable
public class PurchasePK implements Serializable {
#Column(name = "CUSTOMER_ID")
private String customerId;
#Column(name = "PURCHASE_ID")
private String productId;
#Column(name = "TRANSACTION_DATE")
private String date;
}
Conclusion:
The provided information from #Ray was valid, you missed adding the required join columns to represent the full entity relation, regarding your note for the same #Ray point, yes you are right both columns usage is different but also both columns have their own name which it will not override any row value on runtime.
The result of the above tables and representation is as follows:
create table customer
(
customer_id varchar(255) not null,
purchase_date varchar(255) not null,
purchase_id varchar(255) not null,
name varchar(255),
primary key (customer_id, purchase_date, purchase_id)
);
create table purchase
(
customer_id varchar(255) not null,
transaction_date varchar(255) not null,
purchase_id varchar(255) not null,
transaction_name varchar(255),
purchase_date varchar(255),
primary key (customer_id, transaction_date, purchase_id)
);
alter table purchase
add constraint FK6rkrb8rq8x56kai7g5gm32d1y foreign key (customer_id, purchase_date, purchase_id) references customer;
I am trying to create a OneToMany mapping with a LinkedHashMap for my main entity B, that contains the entities VC and P, but I am getting the following error:
Repeated column in mapping for collection:
com.test.model.B.pricing column: b_name
I could be wrong, but I believe that it has something to do with the #JoinColumns or #MapKeyJoinColumn annotation, as I have not done anything like this before, so I am quite sure that I am doing this part incorrectly.
My goal is that I should be able to provide the three fields:
b_name pc and c_id e.g. the VC/VCId
in order to get the a and d_a e.g. P.
Also, if there is a better way to structure things, then I am all ears, as I personally do not really like how I have set up my tables tbh (would be nice if I could just have the b and b_p tables, where the b_p could just have all five fields (key and value) from the p map).
Here is my main entity
#Setter
#Getter
#Entity
#Table(name = "b")
public class B implements Serializable {
#Id
#Column(nullable = false)
private String name;
#OneToMany(cascade = CascadeType.PERSIST)
#JoinTable(
name = "b_p",
joinColumns = #JoinColumn(name = "b_name", referencedColumnName = "name"))
#MapKeyJoinColumns({
#MapKeyJoinColumn(name = "b_name"),
#MapKeyJoinColumn(name = "p_c"),
#MapKeyJoinColumn(name = "c_id")
})
private Map<VC, Price> pricing = new LinkedHashMap<>();
...
}
The Key to the map
#Setter
#Getter
#NoArgsConstructor
#AllArgsConstructor
#Entity
#Table(name = "v_c")
public class VC implements Serializable {
#EmbeddedId private VCId vcId;
}
The key's PK/Composite Key
#Setter
#Getter
#NoArgsConstructor
#AllArgsConstructor
#Embeddable
public class VCId implements Serializable {
#Column(name = "b_name")
private String bName;
#Column(name = "p_c")
private SomeEnum pc;
#Column(name = "c_id")
private String cId;
}
The value for the map
#Setter
#Getter
#NoArgsConstructor
#Embeddable
#Entity
#Table(name = "price")
public class Price implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private long id;
#Column(name = "amount")
private BigDecimal amount;
#Column(name = "discount_amount")
private BigDecimal discountAmount;
}
DB tables
CREATE TABLE b
(
name VARCHAR(100) NOT NULL PRIMARY KEY
...
);
CREATE TABLE v_c
(
bundle_name VARCHAR(100) NOT NULL,
physical_currency TEXT NOT NULL,
coin_id VARCHAR(50) NOT NULL,
FOREIGN KEY (b_name) REFERENCES b (name) ON DELETE CASCADE,
PRIMARY KEY (b_name, p_c, c_id)
);
CREATE TABLE p
(
id BIGSERIAL NOT NULL PRIMARY KEY,
amount NUMERIC,
discount_amount NUMERIC DEFAULT 0.00
);
CREATE TABLE b_p
(
bname VARCHAR(100) NOT NULL,
p_c TEXT NOT NULL,
c_id VARCHAR(50) NOT NULL,
price_id BIGSERIAL NOT NULL,
FOREIGN KEY (b_name, p_c, c_id) REFERENCES v_c (b_name, p_c, c_id) ON DELETE CASCADE,
FOREIGN KEY (price_id) REFERENCES price (id) ON DELETE CASCADE,
PRIMARY KEY (b_name, p_c, c_id)
);
To prevent repeated mapping error, you just have to specify which join should update the column like such:
#OneToMany(cascade = CascadeType.PERSIST)
#JoinTable(name = "bundle_pricing",
joinColumns = #JoinColumn(name = "bundle_name",
referencedColumnName = "name"))
#MapKeyJoinColumns({
#MapKeyJoinColumn(name = "bundle_name", insertable = false, updatable = false),
#MapKeyJoinColumn(name = "physical_currency"), #MapKeyJoinColumn(name = "coin_id")})
private Map<VirtualCurrency, Price> pricing = new LinkedHashMap<>();
NOTE the insertable = false, updatable = false for bundle_name in the MapKeyJoinColumn
remove the #MapKeyJoinColumn(name = "bundle_name") from MapKeyJoinColumns, because when we are creating the JoinColumn in JoinTable it will create the column we don't need to mention it again.
#JoinTable(
name = "bundle_pricing",
joinColumns = #JoinColumn(name = "bundle_name", referencedColumnName = "name"))
#MapKeyJoinColumns({
#MapKeyJoinColumn(name = "physical_currency"),
#MapKeyJoinColumn(name = "coin_id")
})
I have a Hibernate entity.
#AllArgsConstructor #NoArgsConstructor #Getter
#Entity
#Table(name = "app_category_link", schema = "mariott_application")
public class AppCategory {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "app_category_link_id")
private Long id;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "app_id")
private App app;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "category_id")
private Category category;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "user_id_who_added")
private User userWhoAdded;
#Column(name = "date_add")
private ZonedDateTime dateAdded;
}
And I have a #DataJpaTest that generated such DDL. Surprisingly it produdes an unexpected primary key.
create table mariott_application.app_category_link
(
app_category_link_id int8 not null,
date_add timestamp,
app_id bigserial not null,
category_id int8 not null,
user_id_who_added int8,
primary key (app_id, category_id) -- wrong
)
Why does Hibernate generate wrong primary key?
This can happen when you use the table name multiple times e.g. also in a #JoinTable/#CollectionTable. Always make use of an existing entity as inverse #OneToMany rather than defining a #ManyToMany association to avoid this issue.
I have spring-boot application with this two JPA entities joined by one-to-many relation.
#Entity
#Table(name = "todos")
public class ToDo {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column
private String description;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "user_id")
private User user;
#Column
private LocalDate targetDate;
public ToDo() {
}
// Constructors, getters, setter, etc.
}
#Entity
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(unique = true, nullable = false)
private String username;
#Column(nullable = false)
private String password;
#OneToMany(
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<ToDo> toDos = new ArrayList<>();
// Constructors, getters, setter, etc.
}
For such kind of relation I expect to have only two tables but Hibernate create three.
Hibernate: create table todos (id bigserial not null, description varchar(255), target_date date, user_id int8, primary key (id))
Hibernate: create table users (id bigserial not null, password varchar(255) not null, username varchar(255) not null, primary key (id))
Hibernate: create table users_to_dos (user_id int8 not null, to_dos_id int8 not null)
The last one look useless. Why it created and could I prevent that? May be something is wrong in code?
Since your toDos list on the user entity does not have a #JoinColumn annotation, hibernate assumes there's an additional mapping table for this and creates it.
Add the #JoinColumn annotation to the field and it will not be created
See more on this guide
#OneToMany(mappedBy="speciality",fetch = FetchType.LAZY)
#JsonIgnore
private Set<DepartmentMaster> departmentSpeciality;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name="SPECALITY_ID")
#JsonIgnore
private SpecialityMaster speciality;
Try to do the mapping like this I don't think it should create any problem.
I am attempting to map two Java classes using Hibernate. When I compile I get the following error:
Caused by: org.h2.jdbc.JdbcSQLException: Column "COMMENTS0_.DELETED" not found;
Have compared to many examples and everything seems to be correct but there is obviously an error in my mapping. Here is my code:
SQL
-- Table 'TEST_STEP_COMMENT'
CREATE TABLE IF NOT EXISTS `TEST_STEP_COMMENT` (
`id` BIGINT NULL DEFAULT NULL AUTO_INCREMENT,
`test_step_comment` TEXT NOT NULL,
`date` DATETIME NOT NULL,
`test_step_id` BIGINT NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_TEST_STEP_COMMENT_TEST_STEPS`
FOREIGN KEY (`test_step_id`)
REFERENCES `TEST_STEPS` (`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT);
CREATE INDEX `FK_TEST_STEP_COMMENT_TEST_STEPS_idx` ON `TEST_STEP_COMMENT` (`test_step_id` ASC);
CREATE TABLE IF NOT EXISTS `TEST_STEPS` (
`id` BIGINT NULL DEFAULT NULL AUTO_INCREMENT,
`deleted` BOOLEAN NULL DEFAULT FALSE,
`execute` LONGTEXT NOT NULL,
`sequence_order` INT NOT NULL,
`test_case_id` BIGINT NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_TEST_STEPS_TEST_CASES`
FOREIGN KEY (`test_case_id`)
REFERENCES `TEST_CASES` (`id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT);
CREATE INDEX `FK_TEST_STEPS_TEST_CASES_idx` ON `TEST_STEPS` (`test_case_id` ASC);
JAVA
#Entity
#Audited
#Table(name = "TEST_STEPS")
public class TestStep
extends AuditedEntity
implements Identifiable<Long>, Ordered<Integer>, Comparable<TestStep> {
#Id
#GeneratedValue
private Long id;
#ManyToOne(optional = false)
#JoinColumn(name = "test_case_id")
private TestCase testCase;
#Column(name = "execute", nullable = false)
private String execute;
#OneToMany(mappedBy = "testStep", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
#Filter(name = "deletedEntityFilter")
private Set<VerifyStep> verifications = Sets.newLinkedHashSet();
#Column(name = "sequence_order", nullable = false)
private Integer sequenceOrder = 0;
#OneToMany(mappedBy = "testStep", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private Set<TestStepComment> comments = Sets.newLinkedHashSet();
#Column(name = "result")
private Integer result;
#Column(name = "data")
private String data;
//getters/setters etc
}
#Entity
#Audited
#Table(name = "TEST_STEP_COMMENT")
public class TestStepComment
extends AuditedEntity
implements Identifiable<Long>{
#Id
#GeneratedValue
private Long id;
#ManyToOne(optional = false)
#JoinColumn(name = "test_step_id")
private TestStep testStep;
#Column(name = "test_step_comment")
private String comment;
#Column(name = "date")
private Date date;
#Override
public Long getId() {
return id;
//getters/setters etc
}
If anyone has any suggestions of what may be causing my problem it would be much appreciated because my mapping seems to be correct based on multiple examples that I have looked at. Thanks!