rislah
rislah

Reputation: 197

OneToMany bidirectional mapping, missing column

I can't figure out what's the problem. For some reason Hibernate is throwing error about "missing route_id column in bus table". If I create the column and let it be empty then everything works as I want. Why does it need it? I dont need it. Maybe someone can help me, thanks in advance

@Entity
@Getter
@Setter
@Table(name = "bus")
public class Bus {
    @Id
    @Column(name = "bus_id", columnDefinition = "VARCHAR(36)")
    private String id = UUID.randomUUID().toString();

    @NotBlank
    @Length(min = 4, max = 15)
    private String name;

    @OneToMany(mappedBy = "bus", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Set<Route> routes;
}

CREATE TABLE bus
(
    id     BIGINT       NOT NULL AUTO_INCREMENT PRIMARY KEY,
    bus_id VARCHAR(36)  NOT NULL UNIQUE,
    name   VARCHAR(255) NOT NULL UNIQUE
);
@Entity
@Getter
@Setter
@Table(name = "route")
@SecondaryTables({
        @SecondaryTable(name = "location"),
        @SecondaryTable(name = "bus")
})
public class Route {
    @Id
    @Column(name = "route_id", columnDefinition = "VARCHAR(36)")
    private String id = UUID.randomUUID().toString();

    private int leaving;
    private int arrival;
    @Column(columnDefinition = "DECIMAL", precision = 4, scale = 2)
    private BigDecimal price;

    @ManyToOne
    @JoinColumn(name = "bus_id", referencedColumnName = "bus_id")
    private Bus bus;

    @ManyToOne
    @JoinColumn(name = "route_to", referencedColumnName = "name")
    private Location routeTo;

    @ManyToOne
    @JoinColumn(name = "route_from", referencedColumnName = "name")
    private Location routeFrom;
}
CREATE TABLE route
(
    id         BIGINT        NOT NULL AUTO_INCREMENT PRIMARY KEY,
    route_id   VARCHAR(36)   NOT NULL UNIQUE,
    bus_id     VARCHAR(36)   NOT NULL,
    route_from VARCHAR(255)  NOT NULL,
    route_to   VARCHAR(255)  NOT NULL,
    leaving    INT(4) UNSIGNED,
    arrival    INT(4) UNSIGNED,
    price      DECIMAL(4, 2) NOT NULL,
    FOREIGN KEY (bus_id) REFERENCES bus (bus_id) ON DELETE CASCADE,
    FOREIGN KEY (route_from) REFERENCES location (name) ON UPDATE CASCADE ON DELETE CASCADE,
    FOREIGN KEY (route_to) REFERENCES location (name) ON UPDATE CASCADE ON DELETE CASCADE
);

Upvotes: 0

Views: 149

Answers (1)

Lesiak
Lesiak

Reputation: 25936

You have an excessive @SecondaryTable(name = "bus") on your Route entity.

At the same time you have @ManyToOne relationship to bus:

@ManyToOne
@JoinColumn(name = "bus_id", referencedColumnName = "bus_id")
private Bus bus;

This tells me that the secondary table makes no sense - it is used only for situations where ONE entity is split between two tables (example: main table and details table). Here you clearly have two entities.

The same remark goes to @SecondaryTable(name = "location") - you need to remove this annotation.

See Hibernate Tips: How to map an entity to multiple tables for more info on the proper use of @SecondaryTable

Upvotes: 1

Related Questions