question to many-to-many of hibernate

M

Michael Plate

hello

i have three db tables and the first two tables contains the masterdatas.
last table is a mapping table for the both masterdata tables.

CREATE TABLE IF NOT EXISTS MASTERDATA_NO1
(
id bigint not null,
description varchar(255) not null,
primary key (id)
);

CREATE TABLE IF NOT EXISTS MASTERDATA_NO2
(
id bigint not null,
description varchar(255) not null,
value int not null,
primary key (id)
);

CREATE TABLE IF NOT EXISTS MAPPING_MD1_MD2
(
FK_MD1_ID bigint not null,
FK_MD2_ID bigint not null,
primary key (FK_MD1_ID, FK_MD2_ID)
);

my mapping file for hibernate is
.....
<class name="Masterdata1" table="masterdata_no1" >

<id name="id" column="id">
<generator class="increment"/>
</id>

<property name="description" column="description" />

<set name="md2" table="mapping_md1_md2">
<key column="FK_MD1_ID"/>
<many-to-many class="Masterdata2" column="FK_MD2_ID" />
</set>

</class>
...

my java classes are ...

package at.plate.michael.hibernate.valueobject;

import java.util.HashSet;
import java.util.Set;

public class Masterdata1 {
//properties
private int id;
private String description;
private Set<Masterdata2> md2 = new HashSet<Masterdata2>();

//getter and setter for properties
public String getDescription() { return description; }
public int getId() { return id; }
public void setDescription(String description) { this.description =
description; }
public void setId(int id) { this.id = id; }
public Set<Masterdata2> getMd2() { return md2; }
public void setMd2(Set<Masterdata2> md2) { this.md2 = md2; }
}

.... and ...

package at.plate.michael.hibernate.valueobject;

public class Masterdata2 {
//properties
private int id;
private String description;
private int value;

//getter and setter for properties
public String getDescription() { return description; }
public int getId() { return id; }
public void setDescription(String description) { this.description =
description; }
public void setId(int id) { this.id = id; }
....
}

at the moment this runs without a problem. i can store and load Masterdata1
objects with sets of Masterdata2 objects about hibernate to/from database.

yet, i moved the property field 'value' from Masterdata2 to Mapping-Table
and this content should be inserted in masterdata2 object.

how i can modify the mapping file of hibernate to solved this problem?

many thanks and best regards
Michael Plate
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top