简体   繁体   English

休眠连接列

[英]Hibernate joining columns

I'm trying to create join columns in 2 tables in MySQL DB using Hibernate, however, I can't seem to get it working. 我正在尝试使用Hibernate在MySQL DB的2个表中创建联接列,但是,我似乎无法使其正常运行。 I'm really new to this, so maybe I didn't understand it correctly from looking it up earlier. 我真的很陌生,所以也许我从较早的查找中无法正确理解它。

I'm creating a quiz project for my school. 我正在为我的学校创建一个测验项目。 The teacher is able to create tasks. 教师能够创建任务。 I created an ER diagram for this. 我为此创建了一个ER图 If there is anything I should change about it, I'm open to all suggestions. 如果有任何我需要更改的地方,我欢迎所有建议。

It was getting kinda messy so I just created a new project (but the idea is the same), just so it's easier to not get lost in the code. 情况变得有些混乱,所以我刚刚创建了一个新项目(但想法是相同的),这样就更容易避免代码丢失。 I'm trying to join the table Box (Teacher in the project) and Stuff (Tasks in the project). 我试图加入表Box(项目中的老师)和Stuff(项目中的任务)。 Just a quick note, I have tried some variations with the annotations, but it didn't work either. 简要说明一下,我尝试使用批注进行一些变体,但也没有用。

Box class 箱类

@Entity
public class Box {

private int size, box_id;

private String name, shape, colour, material;

public Box(String name, int size, String shape, String colour, String material) {
    this.name = name;
    this.size = size;
    this.shape = shape;
    this.colour = colour;
    this.material = material;
}

public Box() {

}

@Id
@GenericGenerator(name="id" , strategy="increment")
@GeneratedValue(generator="id")
public int getBox_id() {
    return box_id;
}

public void setBox_id(int box_id) {
    this.box_id = box_id;
}

@Column(nullable = false)
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Column(nullable = false)
public int getSize() {
    return size;
}

public void setSize(int size) {
    this.size = size;
}

@Column(nullable = false)
public String getShape() {
    return shape;
}

public void setShape(String shape) {
    this.shape = shape;
}

@Column(nullable = false)
public String getColour() {
    return colour;
}

public void setColour(String colour) {
    this.colour = colour;
}

@Column(nullable = false)
public String getMaterial() {
    return material;
}

public void setMaterial(String material) {
    this.material = material;
}
}

Stuff class 东西类

 @Entity
    public class Stuff {

        private int amount, stuff_id;

        private String name, type, colour, material;

        @ManyToOne
        @JoinColumn(name = "stuff_id")

        private Box box;

        public Stuff(String name, int amount, String type, String colour, String material, Box box) {
            this.name = name;
            this.amount = amount;
            this.type = type;
            this.colour = colour;
            this.material = material;
            this.box = box;
        }

        public Stuff() {
        }

        @Id
        @GenericGenerator(name="id" , strategy="increment")
        @GeneratedValue(generator="id")
        public int getStuff_id() {
            return stuff_id;
        }

        public void setStuff_id(int stuff_id) {
            this.stuff_id = stuff_id;
        }

        @Column(nullable = false)
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Column(nullable = false)
        public int getAmount() {
            return amount;
        }

        public void setAmount(int amount) {
            this.amount = amount;
        }

        @Column(nullable = false)
        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        @Column(nullable = false)
        public String getColour() {
            return colour;
        }

        public void setColour(String colour) {
            this.colour = colour;
        }

        @Column(nullable = false)
        public String getMaterial() {
            return material;
        }

        public void setMaterial(String material) {
            this.material = material;
        }

        @Column(nullable = false)
        public Box getBox() {
            return box;
        }

        public void setBox(Box box) {
            this.box = box;
        }
        }

After that, I just try to put 3 instances of Box and 9 instances of Stuff into the DB, like this. 之后,我只想像这样将3个Box实例和9个Stuff实例放入数据库中。

new BoxDao().instertBox(new Box("box1", 10, "cube", "green", "cardboard"));
new StuffDao().instertStuff(new Stuff("stuff1", 5, "toys", "red", "plastic", new BoxDao().getBoxById(1)));

The error I'm getting is this: 我得到的错误是这样的:

Caused by: org.hibernate.MappingException: Could not determine type for: model.Box, at table: Stuff, for columns: [org.hibernate.mapping.Column(box)]

I appreciate every answer, sorry if it's something obvious but I must have just missed what's wrong. 我很感谢每一个答案,如果很明显,我很抱歉,但我一定只是错过了哪里出了问题。 Thank you. 谢谢。

You must be consistent in where you place the mapping annotations in an entity. 在实体中放置映射注释的位置必须保持一致。 Either you put them all on getters, or you put them all on fields. 您要么将它们全部放在吸气剂上,要么将它们全部放在字段上。

Since you put the Id annotation of the getter, but the ManyToOne annotation on the field, the ManyToOne is ignored by Hibernate. 由于您将getter的Id批注,但将ManyToOne批注放在字段上,因此Hibernate会忽略ManyToOne。

Note that the mapping doesn't make much sense: the column used to uniquely identify a stuff can't be also used to reference a box. 请注意,映射没有太大意义:用于唯一标识物料的列也不能用于引用框。 And the box property can't be annotated with Column and with JoinColumn at the same time. 并且box属性不能同时用Column和JoinColumn进行注释。 It's a JoinColumn, not a Column. 这是一个JoinColumn,而不是Column。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM