简体   繁体   English

在休眠状态下没有外键的一对多

[英]one-to-many without foreign key in hibernate

I have 2 classes Meal and Day. 我有2节课用餐和日。 One Day can have several Meals and one Meal can be served on several Days. 一天可以吃几顿饭,一天可以送几顿饭。 I store a Day object with all the meals from a day. 我存储了一天的物品,包括一天的所有餐点。 And a Meal object with no attribute of the type Day. 还有一个没有Day类型属性的Meal对象。 Code looks as follows: 代码如下:

public class Day {
Date date;
private Map<Meal, List<Integer>> mealsLines;

public Day() {
} ....
}


public class Meal implements java.io.Serializable {
private long id;
private String name;

public Meal() {
} ....


<hibernate-mapping>
<class name="data.Day" table="DAY">
    <id name="date" type="java.sql.Date" access="field">
        <column name="DATE" />
        <generator class="assigned" />
    </id>
    <map name="mealsLines" table="MEAL" lazy="true" access="field">
        <key>
            <column name="DATE" />
        </key>
        <map-key type="data.Meal"></map-key>
        <one-to-many class="data.Meal" />
    </map>
</class>
</hibernate-mapping>


<hibernate-mapping>
<class name="data.Meal" table="MEAL">
    <id name="id" type="long">
        <column name="ID" />
        <generator class="identity" />
    </id>
    <property name="name" type="java.lang.String" access="field">
        <column name="NAME" />
    </property>
</class>
</hibernate-mapping>

So now with these maps I get two Tables: 所以现在使用这些地图我得到两个表:
Meal 膳食
ID NAME ----> DATE <---- wrong ID NAME ----> DATE <----错误

Day
DATE 日期

Which is nonsense because one meal can be severed on more than one day. 这是胡说八道,因为一顿饭可以超过一天被切断。 I dont want a foreign key in meal. 吃饭时我不想要外键。 How can I realise this? 我怎么能意识到这一点? Thanks 谢谢

You can't. 你不能。 By having one meal on many days and many meals on one day, you must have a many-to-many relationship. 通过一天吃一顿饭和一天吃多餐,你必须有多对多的关系。

In order to do this, you must have an associative table between the Meal and the Day tables. 为此,您必须在Meal和Day表之间有一个关联表。

By default, Hibernate will try to use Foreign Keys on the tables if it can because it allows the database to help ensure data consistency and correctness, and is also often faster due to implicit indices that most relational DBs create with foreign keys. 默认情况下,如果可以,Hibernate将尝试在表上使用外键,因为它允许数据库帮助确保数据的一致性和正确性,并且由于大多数关系数据库使用外键创建的隐式索引,因此通常也会更快。

What you are asking is a one-to-many , while from your description it appears that many to many relation is required. 你要问的是一对多,而你的描述似乎需要多对多的关系。 The way you could implement is by creating another mapping class/table which will map foreign keys of other two tables. 您可以实现的方法是创建另一个映射类/表,它将映射其他两个表的外键。

MEAL
╔════════════╗
║ ID* | NAME ║
╠════════════╣
║ 1   | M1   ║
║ 2   | M2   ║
╚════════════╝

DAY
╔════════════╗
║ ID* | DESC ║
╠════════════╣
║ D1  | aa   ║
║ D2  | bb   ║
╚════════════╝

MEAL_DAY_MAPPING

╔══════════╦═════════╦══════════════════╗
║ MEAL_ID* ║ DAY_ID* ║ MEAL_DAY_DETAILS ║
╠══════════╬═════════╬══════════════════╣
║        1 ║ D1      ║ XXX              ║
║        1 ║ D2      ║ YYY              ║
║        2 ║ D1      ║ ZZZ              ║
╚══════════╩═════════╩══════════════════╝

* indicates PK

Ref 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM