O
O
Oleg Gamega2015-01-16 03:47:20
Java
Oleg Gamega, 2015-01-16 03:47:20

Hibernate ManyToOne how to determine the type?

Hello.
I have two tables, I'm trying to relate them with a one-to-many relationship ─Category @OneToMany Content

@Entity
@Table(name = "categorys")
public class Category {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id")
    private Long id;
    @Column(name = "is_active", columnDefinition = "boolean default  true")
    private boolean isActive;
    @Column(name = "title")
    private String title;
    @Column(name = "subtitle")
    private String subtitle;


    private List<Content> contents;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSubtitle() {
        return subtitle;
    }

    public void setSubtitle(String subtitle) {
        this.subtitle = subtitle;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "category")
    public List<Content> getContents() {
        return contents;
    }

    public void setContents(List<Content> contents) {
        this.contents = contents;
    }
}

@Entity
@Table(name = "contents")
public class Content {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id")
    private Long id;
    @Column(name = "content")
    private String content;


    private Category category;


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @ManyToOne( fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id", nullable = false)
    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }
}

When creating tables, I get
org.hibernate.MappingException: Could not determine type for: net.codejava.spring.model.Category, at table: contents, for columns: [org.hibernate.mapping.Column(category)]

As I understand it, hibernate cannot figure out what type to make the category column

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zildarius, 2015-01-16
@gadfi

Can it be better to put annotations on the field, and not on the method?

@ManyToOne( fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private Category category;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question