Honza Štefánik
Honza Štefánik

Reputation: 43

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. 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. 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). 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.

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.

Upvotes: 0

Views: 59

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

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.

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. It's a JoinColumn, not a Column.

Upvotes: 1

Related Questions