-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Primary Key - Foreign Key - composite-id -many-to-many Probl
PostPosted: Tue Jul 10, 2007 7:05 pm 
Newbie

Joined: Tue Jul 10, 2007 6:30 pm
Posts: 2
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=?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 10, 2007 8:35 pm 
Newbie

Joined: Tue Jul 10, 2007 6:30 pm
Posts: 2
I modified my ShiftSchedule mapping but mi problem now is the following:

<?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" inverse="true" lazy="false" cascade="all" table="SHIFT_SCHEDULE" >
<key>
<column name="IDCOMPANY" />
<column name="SHIFT_ID" />
</key>
<many-to-many class="pkg1.Schedule">
<column name="IDCOMPANY" />
<column name="SHEDULE_ID" />
</many-to-many>
</set>

</class>
</hibernate-mapping>

My error now is :
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

org.hibernate.MappingException: Repeated column in mapping for collection: pkg1.schedules column: IDCOMPANY

at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:290)

at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:313)

at org.hibernate.mapping.Collection.validate(Collection.java:270)

at org.hibernate.mapping.Set.validate(Set.java:19)

at org.hibernate.cfg.Configuration.validate(Configuration.java:988)

at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1169)

at FirstExample.main(FirstExample.java:21)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.