Okay,
This is the code of my PageDTO class.
Code:
@Entity
@Table(name = "page")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class PageDTO extends DTO {
private CategoryDTO category;
private Date dateCreation;
private Integer height;
private String name;
// Constructor
public PageDTO() {
}
// Methods
@OneToMany(mappedBy = "parent")
public List<PageDTO> getChildren() {
return this.children;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "category_id")
public CategoryDTO getCategory() {
return this.category;
}
@Temporal(TemporalType.DATE)
@Column(name = "pag_date_creation", nullable = false)
public Date getDateCreation() {
return this.dateCreation;
}
@Basic
@Column(name = "pag_height", nullable = false)
public Integer getHeight() {
return this.height;
}
@Basic
@Column(name = "pag_name", nullable = false, length = 50)
public String getName() {
return this.name;
}
public void setCategory(CategoryDTO category) {
this.category = category;
}
public void setDateCreation(Date dateCreation) {
this.dateCreation = dateCreation;
}
public void setHeight(Integer height) {
this.height = height;
}
public void setName(String name) {
this.name = name;
}
}
Now. This is the code of CategoryDTO class
Code:
@Entity
@Table(name="category")
public class CategoryDTO extends DTO {
// Atributos da Classe
private static final long serialVersionUID = -776137723692955756L;
private String description;
// Construtors
public CategoryDTO() {
}
public CategoryDTO(String description) {
this.setDescription(description);
}
//Métodos
@Basic
@Column(name="cat_description", nullable = false, length = 50)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
}
DTO class contains the attribute named code;
I want insert two pages in same category!!
page1 -> category1
page2 -> category1 // Key violation here!!
example:
Code:
CategoryDTO cat = new CategoryDTO("test")
cat.setCode(1);
PageDTO p1 = new PageDTO();
pageDTO p2 = new PageDTO();
p1.setCategory(cat);
p2.setCategory(cat);
HibernateUtil.store(p1); // Work
HibernateUtil.store(p2); // Key violation in category
Tanks!!