Witam,
Nie udaje mi się zmusić EclipseLink do wygenerowania kaskady dla dwóch encji (relacja jeden do wielu), docelowo do PostgreSQL. Bardzo proszę o podpowiedź co robię źle.
Encje, które napisałem:
package pl.test.entities;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.annotations.CascadeOnDelete;
@Entity
@Table(name = "category")
@CascadeOnDelete
@XmlRootElement
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "category_seq", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "category_seq", sequenceName = "category_seq", allocationSize = 1)
@Column(name = "id")
@NotNull
private Long id;
@Column(name = "name", length = 40, nullable = false)
@NotNull
@Size(min = 3, max = 40)
@Basic(optional = false)
private String name;
@CascadeOnDelete
@OneToMany(fetch = FetchType.LAZY, mappedBy = "category")
private Set<Item> items;
public Category() {
}
/*
* getters and setters
*/
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Item> getItems() {
return items;
}
public void setItems(Set<Item> items) {
this.items = items;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Category)) {
return false;
}
Category other = (Category) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "pl.test.entities.Category[ id=" + id + " ]";
}
}
package pl.test.entities;
import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name = "item")
@XmlRootElement
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "item_seq", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "item_seq", sequenceName = "item_id_seq", allocationSize = 1)
@Column(name = "id")
@NotNull
private Long id;
@Column(name = "name", length = 100, nullable = false, unique = true)
@Basic(optional = false)
@Size(min = 3, max = 100)
@NotNull
private String name;
@Column(name = "description", nullable = false, length = 250)
@Basic(optional = false)
@Size(min = 3, max = 250)
@NotNull
private String description;
@ManyToOne(cascade = CascadeType.REMOVE)
@JoinColumn(name = "id_category", nullable = false)
private Category category;
public Item() {
}
/*
* getters and setters
*/
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Override
public int hashCode() {
int hash = 7;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Item other = (Item) obj;
if (!Objects.equals(this.id, other.id)) {
return false;
}
if (!Objects.equals(this.name, other.name)) {
return false;
}
if (!Objects.equals(this.description, other.description)) {
return false;
}
return true;
}
@Override
public String toString() {
return "pl.test.entities.Item[ id=" + id + " ]";
}
}
Wygenerowany DDL:
CREATE TABLE category
(
id bigint NOT NULL,
name character varying(40) NOT NULL,
CONSTRAINT category_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE category
OWNER TO postgres;
CREATE TABLE item
(
id bigint NOT NULL,
description character varying(250) NOT NULL,
name character varying(100) NOT NULL,
id_category bigint NOT NULL,
CONSTRAINT item_pkey PRIMARY KEY (id),
CONSTRAINT fk_item_id_category FOREIGN KEY (id_category)
REFERENCES category (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT item_name_key UNIQUE (name)
)
WITH (
OIDS=FALSE
);
ALTER TABLE item
OWNER TO postgres;
W encji item chcę otrzymać
ON UPDATE NO ACTION ON DELETE CASCADE
.
Pozdrawiam,