与额外列的一对多休眠关系出错

时间:2018-01-21 15:43:44

标签: java hibernate

我有餐桌:

CREATE TABLE DietMeals (
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
dietId INT(11) UNSIGNED NULL,
mealOrder INT(3) NOT NULL,
name VARCHAR(50) NOT NULL,
color VARCHAR(50) NOT NULL,
CONSTRAINT FK_DietMeals_dietId FOREIGN KEY(dietId) REFERENCES Diets(id)
);

膳食应该有食物清单,但食物不应该知道它们属于哪种食物。食物表:

CREATE TABLE Foods (
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
energy DOUBLE UNSIGNED NULL,
kcal DOUBLE UNSIGNED NULL,
protein DOUBLE UNSIGNED NULL,
fat DOUBLE UNSIGNED NULL,
carbs DOUBLE UNSIGNED NULL,
addedBy INT(11) UNSIGNED NULL,
modifiedBy INT(11) UNSIGNED NULL,
addedDate DATETIME NOT NULL,
modifiedDate DATETIME NULL,
CONSTRAINT FK_Foods_addedBy FOREIGN KEY(addedBy) REFERENCES Users(id),
CONSTRAINT FK_Foods_modifiedBy FOREIGN KEY(modifiedBy) REFERENCES Users(id)
);

我创建了一个表来保存含有额外列的膳食和食物的外键:

CREATE TABLE DietMealFoods (
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
dietMealId INT(11) UNSIGNED NULL,
foodId INT(11) UNSIGNED NULL,
amount DOUBLE UNSIGNED NULL
);

Hibernate类: DietMeal课程:

@Entity(name = "DietMeals")
public class DietMeal implements Serializable {

/**
 * Defautl serialization ID.
 */
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@ManyToOne
@JoinColumn(name="dietId")
private Diet diet;

@Column(name = "name")
private String name;

@Column(name = "color")
private String color;

@Column(name ="mealOrder")
private Integer mealOrder;

@OneToMany(
    mappedBy = "dietMeal",
    orphanRemoval = true
)
private List<DietMealFood> foods = new ArrayList<>();

/**
 * Gets the id.
 * @return the id
 */
public Integer getId() {
    return id;
}

/**
 * Sets the id.
 * @param id the id to set
 */
public void setId(Integer id) {
    this.id = id;
}

/**
 * Gets the diet.
 * @return the diet
 */
public Diet getDiet() {
    return diet;
}

/**
 * Sets the diet.
 * @param diet the diet to set
 */
public void setDiet(Diet diet) {
    this.diet = diet;
}


/**
 * Gets the foods.
 * @return the foods
 */
public List<DietMealFood> getFoods() {
    return foods;
}

/**
 * Sets the foods.
 * @param foods the foods to set
 */
public void setFoods(List<DietMealFood> foods) {
    this.foods = foods;
}

/**
 * Gets the name.
 * @return the name
 */
public String getName() {
    return name;
}

/**
 * Sets the name.
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}

/**
 * Gets the color.
 * @return the color
 */
public String getColor() {
    return color;
}

/**
 * Sets the color.
 * @param color the color to set
 */
public void setColor(String color) {
    this.color = color;
}

/**
 * Gets the mealOrder.
 * @return the mealOrder
 */
public Integer getMealOrder() {
    return mealOrder;
}

/**
 * Sets the mealOrder.
 * @param mealOrder the mealOrder to set
 */
public void setMealOrder(Integer mealOrder) {
    this.mealOrder = mealOrder;
}


}

食品类:

@Entity(name = "Foods")
public class Food extends ModifySupportPersistabe{

/**
 * Defautl serialization ID.
 */
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Column(name = "name")
private String name;

@Column(name = "energy")
private Double energy;

@Column(name = "kcal")
private Double kcal;

@Column(name = "protein")
private Double protein;

@Column(name = "fat")
private Double fat;

@Column(name = "carbs")
private Double carbs;

/**
 * Gets the id.
 * @return the id
 */
public Integer getId() {
    return id;
}

/**
 * Sets the id.
 * @param id the id to set
 */
public void setId(Integer id) {
    this.id = id;
}

/**
 * Gets the name.
 * @return the name
 */
public String getName() {
    return name;
}

/**
 * Sets the name.
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}

/**
 * Gets the energy.
 * @return the energy
 */
public Double getEnergy() {
    return energy;
}

/**
 * Sets the energy.
 * @param energy the energy to set
 */
public void setEnergy(Double energy) {
    this.energy = energy;
}

/**
 * Gets the kcal.
 * @return the kcal
 */
public Double getKcal() {
    return kcal;
}

/**
 * Sets the kcal.
 * @param kcal the kcal to set
 */
public void setKcal(Double kcal) {
    this.kcal = kcal;
}

/**
 * Gets the protein.
 * @return the protein
 */
public Double getProtein() {
    return protein;
}

/**
 * Sets the protein.
 * @param protein the protein to set
 */
public void setProtein(Double protein) {
    this.protein = protein;
}

/**
 * Gets the fat.
 * @return the fat
 */
public Double getFat() {
    return fat;
}

/**
 * Sets the fat.
 * @param fat the fat to set
 */
public void setFat(Double fat) {
    this.fat = fat;
}

/**
 * Gets the carbs.
 * @return the carbs
 */
public Double getCarbs() {
    return carbs;
}

/**
 * Sets the carbs.
 * @param carbs the carbs to set
 */
public void setCarbs(Double carbs) {
    this.carbs = carbs;
}

}

DietMealFood:

@Entity(name = "DietMealFoods")
public class DietMealFood {

@EmbeddedId
private DietMealFoodId id;

@ManyToOne(fetch = FetchType.EAGER)
@MapsId("dietMealId")
private DietMeal dietMeal;

@ManyToOne(fetch = FetchType.EAGER)
@MapsId("foodId")
private Food food;

@Column(name = "amount")
private Double amount;

当我尝试在DietMeal类上调用方法getFoods()时出现此错误:

Caused by: org.mariadb.jdbc.internal.util.dao.QueryException: Unknown column 'foods0_.dietMeal_id' in 'field list'

他为什么要在食品类中检查dietMealId?

堆栈追踪:

    Caused by: java.sql.SQLSyntaxErrorException: (conn:117) Unknown column 'foods0_.dietMeal_id' in 'field list'
Query is: select foods0_.dietMeal_id as dietMeal2_1_0_, foods0_.food_id as food_id3_1_0_, foods0_.dietMeal_id as dietMeal2_1_1_, foods0_.food_id as food_id3_1_1_, foods0_.amount as amount1_1_1_, food1_.id as id1_4_2_, food1_.addedBy as addedBy10_4_2_, food1_.addedDate as addedDat2_4_2_, food1_.modifiedBy as modifie11_4_2_, food1_.modifiedDate as modified3_4_2_, food1_.carbs as carbs4_4_2_, food1_.energy as energy5_4_2_, food1_.fat as fat6_4_2_, food1_.kcal as kcal7_4_2_, food1_.name as name8_4_2_, food1_.protein as protein9_4_2_ from DietMealFoods foods0_ inner join Foods food1_ on foods0_.food_id=food1_.id where foods0_.dietMeal_id=?, parameters [29]
    at org.mariadb.jdbc.internal.util.ExceptionMapper.get(ExceptionMapper.java:139) ~[mariadb-java-client-1.5.9.jar:na]
    at org.mariadb.jdbc.internal.util.ExceptionMapper.getException(ExceptionMapper.java:101) ~[mariadb-java-client-1.5.9.jar:na]
    at org.mariadb.jdbc.internal.util.ExceptionMapper.throwAndLogException(ExceptionMapper.java:77) ~[mariadb-java-client-1.5.9.jar:na]
    at org.mariadb.jdbc.MariaDbStatement.executeQueryEpilog(MariaDbStatement.java:226) ~[mariadb-java-client-1.5.9.jar:na]
    at org.mariadb.jdbc.MariaDbClientPreparedStatement.executeInternal(MariaDbClientPreparedStatement.java:233) ~[mariadb-java-client-1.5.9.jar:na]
    at org.mariadb.jdbc.MariaDbClientPreparedStatement.executeQuery(MariaDbClientPreparedStatement.java:177) ~[mariadb-java-client-1.5.9.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_151]
    at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114) ~[tomcat-jdbc-8.5.16.jar:na]
    at com.sun.proxy.$Proxy64.executeQuery(Unknown Source) ~[na:na]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    ... 94 common frames omitted
Caused by: org.mariadb.jdbc.internal.util.dao.QueryException: Unknown column 'foods0_.dietMeal_id' in 'field list'
Query is: select foods0_.dietMeal_id as dietMeal2_1_0_, foods0_.food_id as food_id3_1_0_, foods0_.dietMeal_id as dietMeal2_1_1_, foods0_.food_id as food_id3_1_1_, foods0_.amount as amount1_1_1_, food1_.id as id1_4_2_, food1_.addedBy as addedBy10_4_2_, food1_.addedDate as addedDat2_4_2_, food1_.modifiedBy as modifie11_4_2_, food1_.modifiedDate as modified3_4_2_, food1_.carbs as carbs4_4_2_, food1_.energy as energy5_4_2_, food1_.fat as fat6_4_2_, food1_.kcal as kcal7_4_2_, food1_.name as name8_4_2_, food1_.protein as protein9_4_2_ from DietMealFoods foods0_ inner join Foods food1_ on foods0_.food_id=food1_.id where foods0_.dietMeal_id=?, parameters [29]
    at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.readErrorPacket(AbstractQueryProtocol.java:1144) ~[mariadb-java-client-1.5.9.jar:na]
    at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.readPacket(AbstractQueryProtocol.java:1076) ~[mariadb-java-client-1.5.9.jar:na]
    at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.getResult(AbstractQueryProtocol.java:1031) ~[mariadb-java-client-1.5.9.jar:na]
    at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.executeQuery(AbstractQueryProtocol.java:203) ~[mariadb-java-client-1.5.9.jar:na]
    at org.mariadb.jdbc.MariaDbClientPreparedStatement.executeInternal(MariaDbClientPreparedStatement.java:224) ~[mariadb-java-client-1.5.9.jar:na]
    ... 102 common frames omitted

0 个答案:

没有答案
相关问题