Дано: есть 2 обьекта. Rabbit и Note. Каждой ноте соответствует один Rabbit . Note может быть много у одного Rabbita
Цель: в класс Note должно подтягиваться Rabbit.name автоматически в поле Note.rabbitname
и при удалении Rabbit все Note что ему соответствуют тоже должны удалится.
до этого у меня работало с помощью
в Note:
many-to-one name="rabbit" column="name" insert="false" update="false"
class="classes.Rabbit"
cascade="save-update"
+ в SQL:
FOREIGN KEY (`name`) REFERENCES `rabbit_table` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT
теперь пытаюсь изучить вариант с аннотациями.
выдает
org.hibernate.AnnotationException: @Column(s) not allowed on a @ManyToOne property: classes.Rabbit.id
@Entity
@Table(name = "rabbit_table")
public class Rabbit {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable=false)
@ManyToOne(cascade = {CascadeType.REMOVE}, targetEntity=Note.class)
@JoinColumn(name = "rabbitName", nullable=false)
private int id;
@Column(name = "name", nullable=false)
private String name;
@Column(name = "sex", nullable=false)
private int sex;
@Column(name = "date_of_birth", nullable=false)
private Date dateOfBirth;
@Column(name = "count", nullable=false)
private int count;
@Column(name = "person_diary")
private String personDiary;
private int age;
public Rabbit(String name, int sex, Date dateOfBirth, int count, String personDiary) {
this.name = name;
this.sex = sex;
this.dateOfBirth = dateOfBirth;
this.count = count;
this.personDiary = personDiary;
}
public Rabbit() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getPersonDiary() {
return personDiary;
}
public void setPersonDiary(String personDiary) {
this.personDiary = personDiary;
}
private int calculateRabbitAge(Date birthDate) {
age=1
return age;
}
@Override
public String toString() {
return "'" + name + "'" + " " + (sex == 1 ? "wooman" : sex == 3 ? "little rabbit" : "man") + " age: "
+ calculateRabbitAge(dateOfBirth) + " month " + count + " count";
}
@Entity(name="note_table")
public class Note implements Comparable<Note> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id", nullable=false)
private int id;
@Column(name="name", nullable=false)
@OneToMany (cascade = {CascadeType.REMOVE}, targetEntity=Rabbit.class, mappedBy = "id", orphanRemoval = true)
private int rabbitName;
@Column(name="text")
private String text;
@Column(name="date_note", nullable=false)
private Date date;
private Rabbit rabbit;
public Note(int rabbitName, String text, Date date) {
this.rabbitName = rabbitName;
this.text = text;
this.date = date;
}
public Note() {
}
public int getRabbitName() {
return rabbitName;
}
public void setRabbitName(int rabbitName) {
this.rabbitName = rabbitName;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Rabbit getRabbit() {
return rabbit;
}
@Override
public int compareTo(Note note) {
return (findDaysLeft() - note.findDaysLeft());
}
public int findDaysLeft() {
daysLeft=1;
return daysLeft;
}
@Override
public String toString() {
return id + ") '" + rabbit.getName() + "' " + text + " data: " + date + " left " + findDaysLeft() + " day";
}
public void setRabbit(Rabbit rabbit) {
this.rabbit = rabbit;
}
}