使用Spring引导的Hibernate上的表列映射错误

时间:2016-12-11 13:18:12

标签: java mysql spring hibernate spring-boot

我在MySql数据库上有两个表。由Item.groupid -> ItemGroup.id关联的Item,ItemGroup我也在使用hibernate-entitymanager和spring boot。这是我的Item和ItemGroup的实体类。

Item.java

package com.thiha.entities;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Item {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String createdDate;
    private String modifiedDate;
    private String itemCode;
    private String description;
    private int groupId;
    private double quantity;
    private double price;
    private int recordStatus;
    private ItemGroup itemGroup;

    @ManyToOne
    @JoinColumn(name = "groupid")
    public ItemGroup getItemGroup() {
        return itemGroup;
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getCreatedDate() {
        return createdDate;
    }
    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }
    public String getModifiedDate() {
        return modifiedDate;
    }
    public void setModifiedDate(String modifiedDate) {
        this.modifiedDate = modifiedDate;
    }
    public String getItemCode() {
        return itemCode;
    }
    public void setItemCode(String itemCode) {
        this.itemCode = itemCode;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public int getGroupId() {
        return groupId;
    }
    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }
    public double getQuantity() {
        return quantity;
    }
    public void setQuantity(double quantity) {
        this.quantity = quantity;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public int getRecordStatus() {
        return recordStatus;
    }
    public void setRecordStatus(int recordStatus) {
        this.recordStatus = recordStatus;
    }
}

ItemGroup.java

package com.thiha.entities;

import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class ItemGroup {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String createdDate;
    private String modifiedDate;
    private String groupCode;
    private String groupName;
    private int recordStatus;
    private List<Item> items;

    @OneToMany(mappedBy = "itemGroup", cascade = CascadeType.ALL)
    public List<Item> getItems(){
        return items;
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getCreatedDate() {
        return createdDate;
    }
    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }
    public String getModifiedDate() {
        return modifiedDate;
    }
    public void setModifiedDate(String modifiedDate) {
        this.modifiedDate = modifiedDate;
    }
    public String getGroupCode() {
        return groupCode;
    }
    public void setGroupCode(String groupCode) {
        this.groupCode = groupCode;
    }
    public String getGroupName() {
        return groupName;
    }
    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }
    public int getRecordStatus() {
        return recordStatus;
    }
    public void setRecordStatus(int recordStatus) {
        this.recordStatus = recordStatus;
    }
}

我还使用外键groupid在Item表中引用ItemGroup表创建外键。但是当我使用mvn spring-boot:run运行代码时,我得到了一些例外情况:

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: item_group, for columns: [org.hibernate.mapping.Column(items)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:431)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:398)
at org.hibernate.mapping.Property.isValid(Property.java:225)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:595)
at org.hibernate.mapping.RootClass.validate(RootClass.java:265)
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)

如果我删除了这两个类中的关系,它正常工作但我无法进行INNER JOIN查询。请帮我解决这个问题

3 个答案:

答案 0 :(得分:2)

您无法在Hibernate中混合注释约定。请尝试将您的@Id@GeneratedValue注释移至getter,或将@OneToMany@ManyToOne移至字段。

答案 1 :(得分:0)

它说

  

无法确定类型:java.util.List,在表:item_group,对于列:[org.hibernate.mapping.Column(items)]'

因为mappedBy不正确。

当你在getter getCategory()上添加注释时,hibernate知道它是category

@ManyToOne
@JoinColumn(name = "groupid")
public ItemGroup getCategory() {
    return itemGroup;
}

因此,请在class ItemGroup

中使用适当的名称
@OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
public List<Item> getItems(){
    return items;
}

答案 2 :(得分:0)

不要担心只有一件事在id上删除了@Id和@generatorValue注释的注释并放入了你的id的getter方法 问题将得到解决.....!