problem mapping , I need in write in the table SHIFT_SCHEDULE but my mapping is updating SCHEDULE table, please help me
This is all my information,
CREATE TABLE COMPANY (
ID VARCHAR2 (10) NOT NULL,
NAME VARCHAR2 (100),
CONSTRAINT PK_COMPANY PRIMARY KEY (ID)
}
CREATE TABLE SCHEDULE (
IDCOMPANY VARCHAR2 (10) NOT NULL,
ID VARCHAR2 (10) NOT NULL,
NAME VARCHAR2 (50) NOT NULL,
CONSTRAINT PK_SCHEDULE PRIMARY KEY (IDCOMPANY,ID)
)
CREATE TABLE SHIFT (
IDCOMPANY VARCHAR2 (10) NOT NULL,
ID VARCHAR2 (10) NOT NULL,
NAME VARCHAR2 (50) NOT NULL,
CONSTRAINT PK_SHIFT PRIMARY KEY ( IDCOMPANY, ID )
)
CREATE TABLE SHIFT_SCHEDULE (
IDCOMPANY VARCHAR2 (10) NOT NULL,
SHIFT_ID VARCHAR2 (10) NOT NULL,
SHEDULE_ID VARCHAR2 (10) NOT NULL,
CONSTRAINT PK_SHIFTSCHEDULE PRIMARY KEY (IDCOMPANY,SHIFT_ID,SHEDULE_ID)
)
My FK for this files are
ALTER TABLE SCHEDULE ADD CONSTRAINT FK_COMPANY_SCHEDULE
FOREIGN KEY (IDCOMPANY)
REFERENCES COMPANY(ID);
ALTER TABLE SHIFT ADD CONSTRAINT FK_COMPANY_SHIFT
FOREIGN KEY (IDCOMPANY)
REFERENCES COMPANY (ID);
ALTER TABLE SHIFT_SCHEDULE ADD CONSTRAINT FK_SHIFT_SCHEDULES
FOREIGN KEY (IDCOMPANY, SHIFT_ID)
REFERENCES SHIFT (IDCOMPANY, ID) ;
ALTER TABLE SHIFT_SCHEDULE ADD CONSTRAINT FK_SHIFT_SHIFT
FOREIGN KEY (IDCOMPANY,SHEDULE_ID)
REFERENCES SCHEDULE (IDCOMPANY, ID) ;
I have the following mapping for this files:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="pkg1.Company" table="COMPANY">
<id name="id" type="string" column="ID" >
<generator class="assigned"/>
</id>
<property name="name">
<column name="NAME" />
</property>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="pkg1.Schedule" table="SCHEDULE">
<composite-id >
<key-many-to-one name="company" lazy="false" class="pkg1.Company" column="IDCOMPANY"/>
<key-property name="id" type="string" column="ID" />
</composite-id >
<property name="name">
<column name="NAME" />
</property>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="pkg1.ShiftSchedule" table="SHIFT">
<composite-id>
<key-many-to-one name="company" lazy="false" class="pkg1.Company" column="IDCOMPANY"/>
<key-property name="id" type="string" column="ID"/>
</composite-id>
<property name="name">
<column name="NAME" />
</property>
<set name="schedules" lazy="false" cascade="all" table="SHIFT_SCHEDULE" >
<key>
<column name="IDCOMPANY" />
<column name="ID" />
</key>
<one-to-many class="pkg1.Schedule"/>
</set>
</class>
</hibernate-mapping>
These are my clasess :
public class Company {
private String id;
private String name;
/*
The Setters and Getters
*/
}
public class ShiftSchedule implements Serializable{
private String id;
private String name;
private Set schedules;
private Company company;
/*
The Setters and Getters
*/
}
public class Schedule implements Serializable{
String id;
String name;
Company company;
/*
The Setters and Getters
*/
}
I have 3 records in Schedule table.
and my code looks like
.
.
.
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
org.hibernate.Transaction tx = session.beginTransaction();
Company company = new Company();
company.setId("001");
company.setName("My Company");
Query query =null;
String select1 ="FROM Schedule";
query = session.createQuery(select1);
List schedules = query.list();
Set x = new HashSet();
x.addAll(schedules);
ShiftSchedule sf=new ShiftSchedule();
sf.setCompany(company);
sf.setId("888");
sf.setName("ABCDEF");
sf.setSchedules(x);
session.saveOrUpdate(sf);
tx.commit();
.
.
.
My SQL GENERATE IS
Hibernate: insert into SHIFT (NAME, IDCOMPANY, ID) values (?, ?, ?)
Hibernate: update SCHEDULE set IDCOMPANY=?, ID=? where IDCOMPANY=? and ID=?
Hibernate: update SCHEDULE set IDCOMPANY=?, ID=? where IDCOMPANY=? and ID=?
Hibernate: update SCHEDULE set IDCOMPANY=?, ID=? where IDCOMPANY=? and ID=?
|