Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

one-to-many without foreign key in hibernate

I have 2 classes Meal and Day. 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. 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

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

like image 994
eclipse Avatar asked Dec 30 '25 16:12

eclipse


2 Answers

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.

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.

like image 172
cdeszaq Avatar answered Jan 02 '26 11:01

cdeszaq


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

  • http://docs.jboss.org/hibernate/core/4.0/manual/en-US/html_single/#d0e11567
  • http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation
like image 31
Prashant Bhate Avatar answered Jan 02 '26 09:01

Prashant Bhate



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!