Hibernate version: 2.17
Name and version of the database you are using: MS SQL Server 2000
I have 3 tables: CD_JOB_PAAK, SCH_TASK_PAAM and SCH_TASK_JOB_PAAD. The CD_JOB_PAAK table contains jobs and SCH_TASK_PAAM table contains tasks defined in the system while the SCH_TASK_JOB_PAAD table is the link table.
/* Setup Tables */
CREATE TABLE [dbo].[CD_JOB_PAAK] (
[job_id] [int] NOT NULL ,
[job_name] [varchar] (50) NOT NULL ) ON [PRIMARY]
GO
CREATE TABLE [dbo].[SCH_TASK_PAAM] (
[task_id] [int] NOT NULL ,
[task_name] [varchar] (50) NOT NULL ) ON [PRIMARY]
GO
CREATE TABLE [dbo].[SCH_TASK_JOB_PAAD] (
[task_job_id] [int] NOT NULL ,
[paam_task_id] [int] NOT NULL ,
[paak_job_id] [int] NOT NULL ON [PRIMARY]
GO
/* Setup Primary Keys */
ALTER TABLE [dbo].[CD_JOB_PAAK] WITH NOCHECK ADD
CONSTRAINT [XPKcd_job_paak] PRIMARY KEY CLUSTERED
([job_id]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[SCH_TASK_PAAM] WITH NOCHECK ADD
CONSTRAINT [XPKsch_task_paam] PRIMARY KEY CLUSTERED
([task_id]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[SCH_TASK_JOB_PAAD] WITH NOCHECK ADD
CONSTRAINT [XPKsch_task_job_paad] PRIMARY KEY CLUSTERED
([task_job_id]) ON [PRIMARY]
GO
/* Setup Foreign Keys */
ALTER TABLE [dbo].[SCH_TASK_JOB_PAAD] ADD
CONSTRAINT [FK_sch_task_job_paad_cd_job_paak]
FOREIGN KEY ([paak_job_id])
REFERENCES [dbo].[CD_JOB_PAAK] ([job_id]),
CONSTRAINT [FK_sch_task_job_paad_sch_task_paam]
FOREIGN KEY ([paam_task_id])
REFERENCES [dbo].[SCH_TASK_PAAM] ([task_id])
GO
/* Setup Unique Indexes */
CREATE UNIQUE INDEX [XAK1sch_task_job_paad] ON [dbo].[SCH_TASK_JOB_PAAD]([paam_task_id], [paak_job_id]) ON [PRIMARY]
These are the POJOs:
CdJob.java
package com.pendylum.main.model;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.builder.ToStringBuilder;
public class CdJob implements Serializable {
/** identifier field */
private Integer jobId;
/** persistent field */
private String jobName;
/** persistent field */
private Set schTaskJobs;
/**
* Default Constructor
*/
public CdJob() {
}
/**
* @return Returns the jobId.
*/
public Integer getJobId() {
return jobId;
}
/**
* @param jobId The jobId to set.
*/
private void setJobId(Integer jobId) {
this.jobId = jobId;
}
/**
* @return Returns the jobName.
*/
public String getJobName() {
return jobName;
}
/**
* @param jobName The jobName to set.
*/
public void setJobName(String jobName) {
this.jobName = jobName;
}
/**
* @return Returns the schTaskJobs.
*/
public Set getSchTaskJobs() {
return schTaskJobs;
}
/**
* @param schTaskJobs The schTaskJobs to set.
*/
public void setSchTaskJobs(Set schTaskJobs) {
this.schTaskJobs = schTaskJobs;
}
public String toString() {
return new ToStringBuilder(this)
.append("jobId", getJobId())
.toString();
}
}
SchTask.java
package com.pendylum.scheduler.model;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;
import org.apache.commons.lang.builder.ToStringBuilder;
import com.pendylum.main.model.CdJob;
public class SchTask implements Serializable {
/** identifier field */
private Integer schTaskId;
/** persistent field */
private String schTaskName;
/** persistent field */
private Set schTaskJobs;
/**
* Default Constructor
*/
public SchTask() {
}
/**
* @return Returns the schTaskId.
*/
public Integer getSchTaskId() {
return schTaskId;
}
/**
* @param schTaskId The schTaskId to set.
*/
private void setSchTaskId(Integer schTaskId) {
this.schTaskId = schTaskId;
}
/**
* @return Returns the schTaskName.
*/
public String getSchTaskName() {
return schTaskName;
}
/**
* @param schTaskName The schTaskName to set.
*/
public void setSchTaskName(String schGroupName) {
this.schTaskName = schGroupName;
}
/**
* @return Returns the schTaskJobs.
*/
public Set getSchTaskJobs() {
return schTaskJobs;
}
/**
* @param schTaskJobs The schTaskJobs to set.
*/
public void setSchTaskJobs(Set schTaskJobs) {
this.schTaskJobs = schTaskJobs;
}
public String toString() {
return new ToStringBuilder(this)
.append("schTaskId", getSchTaskId())
.toString();
}
}
SchTaskJob.java
package com.pendylum.scheduler.model;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
import com.pendylum.main.model.CdJob;
public class SchTaskJob implements Serializable {
/** identifier field */
private Integer schTaskJobId;
/** persistent field */
private SchTask schTask;
/** persistent field */
private CdJob cdJob;
/**
* Default Constructor
*/
public SchTaskJob() {
}
/**
* @return Returns the schTaskJobId.
*/
public Integer getSchTaskJobId() {
return schTaskJobId;
}
/**
* @param schTaskJobId The schTaskJobId to set.
*/
public void setSchTaskJobId(Integer schTaskJobId) {
this.schTaskJobId = schTaskJobId;
}
/**
* @return Returns the schTask.
*/
public SchTask getSchTask() {
return schTask;
}
/**
* @param schTask The schTask to set.
*/
public void setSchTask(SchTask schTask) {
this.schTask = schTask;
}
/**
* @return Returns the cdJob.
*/
public CdJob getCdJob() {
return cdJob;
}
/**
* @param cdJob The cdJob to set.
*/
public void setCdJob(CdJob cdJob) {
this.cdJob = cdJob;
}
public String toString() {
return new ToStringBuilder(this)
.append("schTaskJobId", getSchTaskJobId())
.toString();
}
}
The mapping files are pretty simple :
CdJob.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class
name="com.pendylum.main.model.CdJob"
table="CD_JOB_PAAK"
>
<id name="jobId" type="java.lang.Integer" column="job_id">
<generator class="identity" />
</id>
<property
name="jobName"
type="java.lang.String"
column="job_name"
not-null="true"
unique="true"
length="50"
/>
<!-- bi-directional one-to-many association to SchTaskJob -->
<set
name="schTaskJobs"
lazy="true"
inverse="true"
cascade="none"
>
<key>
<column name="paak_job_id" />
</key>
<one-to-many
class="com.pendylum.scheduler.model.SchTaskJob"
/>
</set>
</class>
</hibernate-mapping>
SchTask.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class
name="com.pendylum.scheduler.model.SchTask"
table="SCH_TASK_PAAM"
>
<id name="schTaskId" type="java.lang.Integer" column="task_id">
<generator class="identity" />
</id>
<property
name="schTaskName"
type="java.lang.String"
column="task_name"
not-null="true"
unique="true"
length="50"
/>
<!-- bi-directional one-to-many association to SchTaskJob -->
<set
name="schTaskJobs"
lazy="true"
inverse="true"
cascade="all"
>
<key>
<column name="paam_task_id" />
</key>
<one-to-many
class="com.pendylum.scheduler.model.SchTaskJob"
/>
</set>
</class>
</hibernate-mapping>
SchTaskJob.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class
name="com.pendylum.scheduler.model.SchTaskJob"
table="SCH_TASK_JOB_PAAD"
>
<id name="schTaskJobId" type="java.lang.Integer" column="task_job_id">
<generator class="identity" />
</id>
<!-- bi-directional many-to-one association to SchTask -->
<many-to-one
name="schTask"
class="com.pendylum.scheduler.model.SchTask"
not-null="true"
>
<column name="paam_task_id" />
</many-to-one>
<!-- bi-directional many-to-one association to CdJob -->
<many-to-one
name="cdJob"
class="com.pendylum.main.model.CdJob"
not-null="true"
>
<column name="paak_job_id" />
</many-to-one>
</class>
</hibernate-mapping>
When I add a new SchTask record, I also add 1 or more corresponding records in SchTaskJob. This is working perfect:
// creates a DAOFactory
SchDAOFactory aSchDAOFactory = SchDatastoreSelector.getSchDAOFactory();
// creates DAOs
SchTaskDAO aSchTaskDAO = aSchDAOFactory.getSchTaskDAO();
CdJobDAO aCdJobDAO = aSchDAOFactory.getCdJobDAO();
SchTask aSchTask = new SchTask();
aSchTask.setSchTaskName("Task 1");
java.util.Set schTaskJobs = new HashSet();
// setup SchTaskJob(s)
Integer[] jobs = {new Integer(1), new Integer(2), new Integer(4)};
for ( int i = 0; i<jobs.length; i++ ) {
CdJob aCdJob = aCdJobDAO.getJobById( jobs[i] );
SchTaskJob aSchTaskJob = new SchTaskJob();
aSchTaskJob.setSchTask( aSchTask );
aSchTaskJob.setCdJob( aCdJob );
schTaskJobs.add( aSchTaskJob );
}
aSchTask.setSchTaskJobs( schTaskJobs );
aSchTask = aSchTaskDAO.saveSchTask( aSchTask );
But when I retrieve a SchTask record and try to drop some of its correponding records in SchTaskJob, nothing happens in the SCH_TASK_JOB_PAAD table. No exception is thrown as well.
Lets say I have the following data:
In CD_JOB_PAAK :
job_id job_name
--------------------
1 Job 1
2 Job 2
3 Job 3
4 Job 4
In SCH_TASK_PAAM :
task_id task_name
----------------------
1 Task 1
In SCH_TASK_JOB_PAAD :
task_job_id paam_task_id paak_job_id
----------------------------------------------
1 1 1
2 1 2
3 1 4
I want to retrieve the "Task 1" data and drops the jobs associated to it.
// creates a DAOFactory
SchDAOFactory aSchDAOFactory = SchDatastoreSelector.getSchDAOFactory();
// creates DAOs
SchTaskDAO aSchTaskDAO = aSchDAOFactory.getSchTaskDAO();
CdJobDAO aCdJobDAO = aSchDAOFactory.getCdJobDAO();
// retrieve SchTask
SchTask aSchTask = aSchTaskDAO.getSchTaskById( new Integer(2) );
java.util.Set schTaskJobs = aSchTask.getSchTaskJobs();
// remove existing SchTaskJob(s)
schTaskJobs.clear();
aSchTask.setSchTaskJobs( schTaskJobs );
aSchTask = aSchTaskDAO.saveSchTask( aSchTask );
Can anybody tell me what is wrong here. Thanks
|