S
S
SankaSanka2019-11-18 13:43:34
Java
SankaSanka, 2019-11-18 13:43:34

How to properly organize links with annotations?

Given: there are 2 objects. Rabbit and Note. Each note corresponds to one Rabbit . One Rabbita can have many Notes
Purpose: Rabbit.name should be automatically added to the Note class in the Note.rabbitname field,
and when the Rabbit is deleted, all Note that correspond to it should also be deleted.
before that it worked for me using
in Note:


many-to-one name="rabbit" column="name" insert="false" update="false"
class="classes.Rabbit"
cascade="save-update"

+ in SQL:
FOREIGN KEY (`name`) REFERENCES `rabbit_table` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT

Now I'm trying to explore the option with annotations.
issues
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;
    }
 
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bonce, 2019-11-18
@Bonce

Look closely at the Rabbit class. You annotate id, but declare the id field in other annotations.

@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;

Should be replaced with:
@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable=false)
    private int id;
    
    @OneToMany(cascade = {CascadeType.REMOVE}, targetEntity=Note.class)
    @JoinColumn(name = "rabbitName", nullable=false)
    private int id;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question