Hi everybody,
I am very new to this hibernate persistence. Saying that parent key violated .
I have a plm. with my hibernate application.
Let me give my HBM files & POJO classes.
This is my Outcome table's hbm file
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Feb 23, 2007 3:30:00 PM by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping><meta attribute='extends'>com.mhhe.common.persistence.PersistentObject</meta>
<class name="com.mhhe.authoring.persistence.Outcome" table="OUTCOME">
<id name="id" type="long" unsaved-value="0">
<column name="ID" precision="10" scale="0" />
<generator class="sequence" >
<param name="sequence">nvlx.outcome_seq_id</param>
</generator>
</id>
<many-to-one name="outcomeType" class="com.mhhe.authoring.persistence.OutcomeType" fetch="select">
<column name="TYPE_ID" precision="10" scale="0" />
</many-to-one>
<many-to-one name="project" class="com.mhhe.authoring.persistence.Project" fetch="select">
<column name="PROJECT_ID" precision="10" scale="0" />
</many-to-one>
<property name="name" type="string">
<column name="NAME" length="25" not-null="true" />
</property>
<property name="outcomeDesc" type="string">
<column name="OUTCOME_DESC" length="100" />
</property>
<property name="isbn" type="string">
<column name="ISBN" length="25" not-null="true" />
</property>
<property name="courseId" type="int">
<column name="COURSE_ID" precision="5" scale="0" not-null="true" />
</property>
<property name="createdBy" type="string">
<column name="CREATED_BY" length="35" not-null="true" />
</property>
<property name="createdOn" type="date">
<column name="CREATED_ON" length="7" not-null="true" />
</property>
<property name="updatedBy" type="string">
<column name="UPDATED_BY" length="35" not-null="true" />
</property>
<property name="updatedOn" type="date">
<column name="UPDATED_ON" length="7" not-null="true" />
</property>
<set name="outcomeStructures" inverse="true" cascade="delete">
<key>
<column name="OUTCOME_ID" precision="10" scale="0" not-null="true" />
</key>
<one-to-many class="com.mhhe.authoring.persistence.OutcomeStructure" />
</set>
</class>
</hibernate-mapping>
*********This is the script for that****
DROP TABLE OUTCOME CASCADE CONSTRAINTS ;
CREATE TABLE OUTCOME (
ID NUMBER (10) NOT NULL,
NAME VARCHAR2 (25) NOT NULL,
OUTCOME_DESC VARCHAR2 (100),
ISBN VARCHAR2 (25) NOT NULL,
COURSE_ID NUMBER (5) NOT NULL,
PROJECT_ID NUMBER (10),
CREATED_BY VARCHAR2 (35) NOT NULL,
CREATED_ON DATE NOT NULL,
UPDATED_BY VARCHAR2 (35) NOT NULL,
UPDATED_ON DATE NOT NULL,
TYPE_ID NUMBER (10),
CONSTRAINT PK_OUTCOME
PRIMARY KEY ( ID )
USING INDEX
TABLESPACE USERS PCTFREE 10
STORAGE ( INITIAL 64K NEXT K PCTINCREASE ))
TABLESPACE USERS
PCTFREE 10 PCTUSED
INITRANS 1 MAXTRANS 255
STORAGE (
INITIAL 64K NEXT K PCTINCREASE
MINEXTENTS 1 MAXEXTENTS 2147483645 )
NOCACHE;
ALTER TABLE OUTCOME ADD CONSTRAINT FK_PROJECT_OUTCOME
FOREIGN KEY (PROJECT_ID)
REFERENCES NVLX.PROJECT (PROJECT_ID) ;
ALTER TABLE OUTCOME ADD CONSTRAINT FK_OUTCOME_TYPE_OUTCOME
FOREIGN KEY (TYPE_ID)
REFERENCES NVLX.OUTCOME_TYPE (ID) ;
*******************************************************
2. This is my OutcomeStructure table's hbm file.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Feb 23, 2007 3:30:00 PM by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping><meta attribute='extends'>com.mhhe.common.persistence.PersistentObject</meta>
<class name="com.mhhe.authoring.persistence.OutcomeStructure" table="OUTCOME_STRUCTURE">
<id name="id" type="long" unsaved-value="0">
<column name="ID" precision="10" scale="0" />
<generator class="sequence" >
<param name="sequence">nvlx.outcome_struct_seq_id</param>
</generator>
</id>
<many-to-one name="outcomeStructure" class="com.mhhe.authoring.persistence.OutcomeStructure" fetch="select">
<column name="PARENT_STRUCT_ID" precision="10" scale="0" />
</many-to-one>
<many-to-one name="projectStructure" class="com.mhhe.authoring.persistence.ProjectStructure" fetch="select">
<column name="PRJ_STRUCT_ID" precision="10" scale="0" />
</many-to-one>
<many-to-one name="outcome" class="com.mhhe.authoring.persistence.Outcome" fetch="select">
<column name="OUTCOME_ID" precision="10" scale="0"/>
</many-to-one>
<property name="structName" type="string">
<column name="STRUCT_NAME" length="35" not-null="true" />
</property>
<property name="structLevel" type="byte">
<column name="STRUCT_LEVEL" precision="2" scale="0" not-null="true" />
</property>
<property name="structLevelSeq" type="byte">
<column name="STRUCT_LEVEL_SEQ" precision="2" scale="0" not-null="true" />
</property>
<property name="isInfoView" type="char">
<column name="IS_INFO_VIEW" length="1" not-null="true" />
</property>
<property name="createdBy" type="string">
<column name="CREATED_BY" length="35" not-null="true" />
</property>
<property name="createdOn" type="date">
<column name="CREATED_ON" length="7" not-null="true" />
</property>
<property name="updatedBy" type="string">
<column name="UPDATED_BY" length="35" not-null="true" />
</property>
<property name="updatedOn" type="date">
<column name="UPDATED_ON" length="7" not-null="true" />
</property>
<set name="outcomeStructures" inverse="true" cascade="delete">
<key>
<column name="PARENT_STRUCT_ID" precision="10" scale="0"/>
</key>
<one-to-many class="com.mhhe.authoring.persistence.OutcomeStructure" />
</set>
<set name="outcomeStructureAtoms" inverse="true" cascade="delete">
<key>
<column name="OUTCOME_STRUCT_ID" precision="10" scale="0"/>
</key>
<one-to-many class="com.mhhe.authoring.persistence.OutcomeStructureAtom" />
</set>
</class>
</hibernate-mapping>
********** This is the table script for OutcomeStructure
DROP TABLE OUTCOME_STRUCTURE CASCADE CONSTRAINTS ;
CREATE TABLE OUTCOME_STRUCTURE (
ID NUMBER (10) NOT NULL,
STRUCT_NAME VARCHAR2 (35) NOT NULL,
STRUCT_LEVEL NUMBER (2) NOT NULL,
STRUCT_LEVEL_SEQ NUMBER (2) NOT NULL,
OUTCOME_ID NUMBER (10),
PARENT_STRUCT_ID NUMBER (10),
IS_INFO_VIEW CHAR (1) NOT NULL,
PRJ_STRUCT_ID NUMBER (10),
CREATED_BY VARCHAR2 (35) NOT NULL,
CREATED_ON DATE NOT NULL,
UPDATED_BY VARCHAR2 (35) NOT NULL,
UPDATED_ON DATE NOT NULL,
CONSTRAINT PK_OUTCOME_STRUCTURE
PRIMARY KEY ( ID )
USING INDEX
TABLESPACE USERS PCTFREE 10
STORAGE ( INITIAL 64K NEXT K PCTINCREASE ))
TABLESPACE USERS
PCTFREE 10 PCTUSED
INITRANS 1 MAXTRANS 255
STORAGE (
INITIAL 64K NEXT K PCTINCREASE
MINEXTENTS 1 MAXEXTENTS 2147483645 )
NOCACHE;
ALTER TABLE OUTCOME_STRUCTURE ADD CONSTRAINT FK_OUTCOME_2_STRUCTURE
FOREIGN KEY (OUTCOME_ID)
REFERENCES NVLX.OUTCOME (ID) ;
ALTER TABLE OUTCOME_STRUCTURE ADD CONSTRAINT FK_OUTCOME_STRUCTURE_2
FOREIGN KEY (PARENT_STRUCT_ID)
REFERENCES NVLX.OUTCOME_STRUCTURE (ID) ;
ALTER TABLE OUTCOME_STRUCTURE ADD CONSTRAINT FK_PRJ_STRUCT_OUTCOME_STRUCT
FOREIGN KEY (PRJ_STRUCT_ID)
REFERENCES NVLX.PROJECT_STRUCTURE (PRJ_STRUCT_ID) ;
**********************************************************
The Outcome structure table has the FK outcome_id with Outcome table's ID...........
Here my coding goes
public Outcome saveOutcome(Outcome outcome) {
DBSessionFactoryLocal dbSessionFactory = (DBSessionFactoryLocal)SpringUtil.getBean(DBSessionFactoryLocal.ID);
DBSession dbSession = dbSessionFactory.currentSession();
Session session = dbSession.getSession();
try {
//If Id is zero then save outcome object and child objects else save only child objects
if(outcome.getId() == 0) {
session.save(outcome);
System.out.println("Saved successfully in saveOutcome ::: "+outcome.getId());
//To create OutcomeStructure
createOutcomeStructureForProject(outcome,"Information Center", 'Y');
createOutcomeStructureForProject(outcome,"Student View", 'N');
createOutcomeStructureForProject(outcome,"Instructor View", 'N');
}
return outcome;
}
finally {
if(dbSessionFactory!=null) {
dbSessionFactory.closeCurrentSession();
}
}
}
private void createOutcomeStructureForProject(Outcome outcome, String outcomeView,char isInfoView) {
DBSessionFactoryLocal dbSessionFactory = (DBSessionFactoryLocal)SpringUtil.getBean(DBSessionFactoryLocal.ID);
DBSession dbSession = dbSessionFactory.currentSession();
Session session = dbSession.getSession();
try {
OutcomeStructure outcomeStruct = new OutcomeStructure();
System.out.println(" Out COME ID :::: "+ outcome.getId());
outcomeStruct.setOutcome(outcome);
outcomeStruct.setCreatedBy(outcome.getCreatedBy());
outcomeStruct.setCreatedOn(outcome.getCreatedOn());
outcomeStruct.setUpdatedBy(outcome.getUpdatedBy());
outcomeStruct.setUpdatedOn(outcome.getUpdatedOn());
outcomeStruct.setStructName(outcomeView);
outcomeStruct.setIsInfoView(isInfoView);
outcomeStruct.setStructLevel((byte)0);
outcomeStruct.setStructLevelSeq((byte)0);
session.save(outcomeStruct);
System.out.println("Saved successfully in createOutcomeStructure ::: "+outcomeStruct.getStructName() +" "+outcomeStruct.getId());
}
finally {
if(dbSessionFactory!=null) {
dbSessionFactory.closeCurrentSession();
}
}
}
The first API is adding the values in the table properly. After it is reaching the Outcome Structure table It's faling to enter the outcome_id column.
Outcome is passed from the first API by the Outcome Object, and i am setting it like outcomeStruct.setOutcome(outcome); .
When testing this API, the first OUTCOME table is getting updated, but it is throwing an error like
**********************
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
Truncated. see log file for complete stacktrace
java.sql.BatchUpdateException: ORA-02291: integrity constraint (NVLX.FK_OUTCOME_2_STRUCTURE) violated - parent key not found
at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10698)
at weblogic.jdbc.wrapper.PreparedStatement.executeBatch(PreparedStatement.java:157)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
Truncated. see log file for complete stacktrace
***********-*****************************
Note : When i was debugging it is passed the second called API's session.save(outcomeStruct); Also i was getting System.out.println's printing.
Can anybody please help me regarding this one....
Also i'll include my POJO classes.
1. package com.mhhe.authoring.persistence;
// Generated Feb 23, 2007 3:30:37 PM by Hibernate Tools 3.2.0.beta8
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import com.mhhe.content.persistence.ContentPersistentObject;
/**
* Outcome generated by hbm2java
*/
public class Outcome extends ContentPersistentObject implements java.io.Serializable {
// Fields
private long id;
private OutcomeType outcomeType;
private Project project;
private String name;
private String outcomeDesc;
private String isbn;
private int courseId;
private Set<OutcomeStructure> outcomeStructures = new HashSet<OutcomeStructure>(0);
// Constructors
/** default constructor */
public Outcome() {
}
/** minimal constructor */
public Outcome(long id, String name, String isbn, int courseId, String createdBy, Date createdOn, String updatedBy, Date updatedOn) {
this.id = id;
this.name = name;
this.isbn = isbn;
this.courseId = courseId;
this.createdBy = createdBy;
this.createdOn = createdOn;
this.updatedBy = updatedBy;
this.updatedOn = updatedOn;
}
/** full constructor */
public Outcome(long id, OutcomeType outcomeType, Project project, String name, String outcomeDesc, String isbn, int courseId, String createdBy, Date createdOn, String updatedBy, Date updatedOn, Set<OutcomeStructure> outcomeStructures) {
this.id = id;
this.outcomeType = outcomeType;
this.project = project;
this.name = name;
this.outcomeDesc = outcomeDesc;
this.isbn = isbn;
this.courseId = courseId;
this.createdBy = createdBy;
this.createdOn = createdOn;
this.updatedBy = updatedBy;
this.updatedOn = updatedOn;
this.outcomeStructures = outcomeStructures;
}
// Property accessors
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public OutcomeType getOutcomeType() {
return this.outcomeType;
}
public void setOutcomeType(OutcomeType outcomeType) {
this.outcomeType = outcomeType;
}
public Project getProject() {
return this.project;
}
public void setProject(Project project) {
this.project = project;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getOutcomeDesc() {
return this.outcomeDesc;
}
public void setOutcomeDesc(String outcomeDesc) {
this.outcomeDesc = outcomeDesc;
}
public String getIsbn() {
return this.isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public int getCourseId() {
return this.courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
public Set<OutcomeStructure> getOutcomeStructures() {
return this.outcomeStructures;
}
public void setOutcomeStructures(Set<OutcomeStructure> outcomeStructures) {
this.outcomeStructures = outcomeStructures;
}
}
2. package com.mhhe.authoring.persistence;
// Generated Feb 23, 2007 3:30:37 PM by Hibernate Tools 3.2.0.beta8
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import com.mhhe.content.persistence.ContentPersistentObject;
/**
* OutcomeStructure generated by hbm2java
*/
public class OutcomeStructure extends ContentPersistentObject implements java.io.Serializable {
// Fields
private long id;
private OutcomeStructure outcomeStructure;
private ProjectStructure projectStructure;
private Outcome outcome;
private String structName;
private byte structLevel;
private byte structLevelSeq;
private char isInfoView;
private Set<OutcomeStructure> outcomeStructures = new HashSet<OutcomeStructure>(0);
private Set<OutcomeStructureAtom> outcomeStructureAtoms = new HashSet<OutcomeStructureAtom>(0);
// Constructors
/** default constructor */
public OutcomeStructure() {
}
/** minimal constructor */
public OutcomeStructure(long id, String structName, byte structLevel, byte structLevelSeq, char isInfoView, String createdBy, Date createdOn, String updatedBy, Date updatedOn) {
this.id = id;
this.structName = structName;
this.structLevel = structLevel;
this.structLevelSeq = structLevelSeq;
this.isInfoView = isInfoView;
this.createdBy = createdBy;
this.createdOn = createdOn;
this.updatedBy = updatedBy;
this.updatedOn = updatedOn;
}
/** full constructor */
public OutcomeStructure(long id, OutcomeStructure outcomeStructure, ProjectStructure projectStructure, Outcome outcome, String structName, byte structLevel, byte structLevelSeq, char isInfoView, String createdBy, Date createdOn, String updatedBy, Date updatedOn, Set<OutcomeStructure> outcomeStructures, Set<OutcomeStructureAtom> outcomeStructureAtoms) {
this.id = id;
this.outcomeStructure = outcomeStructure;
this.projectStructure = projectStructure;
this.outcome = outcome;
this.structName = structName;
this.structLevel = structLevel;
this.structLevelSeq = structLevelSeq;
this.isInfoView = isInfoView;
this.createdBy = createdBy;
this.createdOn = createdOn;
this.updatedBy = updatedBy;
this.updatedOn = updatedOn;
this.outcomeStructures = outcomeStructures;
this.outcomeStructureAtoms = outcomeStructureAtoms;
}
// Property accessors
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public OutcomeStructure getOutcomeStructure() {
return this.outcomeStructure;
}
public void setOutcomeStructure(OutcomeStructure outcomeStructure) {
this.outcomeStructure = outcomeStructure;
}
public ProjectStructure getProjectStructure() {
return this.projectStructure;
}
public void setProjectStructure(ProjectStructure projectStructure) {
this.projectStructure = projectStructure;
}
public Outcome getOutcome() {
return this.outcome;
}
public void setOutcome(Outcome outcome) {
this.outcome = outcome;
}
public String getStructName() {
return this.structName;
}
public void setStructName(String structName) {
this.structName = structName;
}
public byte getStructLevel() {
return this.structLevel;
}
public void setStructLevel(byte structLevel) {
this.structLevel = structLevel;
}
public byte getStructLevelSeq() {
return this.structLevelSeq;
}
public void setStructLevelSeq(byte structLevelSeq) {
this.structLevelSeq = structLevelSeq;
}
public char getIsInfoView() {
return this.isInfoView;
}
public void setIsInfoView(char isInfoView) {
this.isInfoView = isInfoView;
}
public Set<OutcomeStructure> getOutcomeStructures() {
return this.outcomeStructures;
}
public void setOutcomeStructures(Set<OutcomeStructure> outcomeStructures) {
this.outcomeStructures = outcomeStructures;
}
public Set<OutcomeStructureAtom> getOutcomeStructureAtoms() {
return this.outcomeStructureAtoms;
}
public void setOutcomeStructureAtoms(Set<OutcomeStructureAtom> outcomeStructureAtoms) {
this.outcomeStructureAtoms = outcomeStructureAtoms;
}
}
Thanks for your vigilance....... I expect a god reply from yours......[/b]
_________________ Regards
Manickaraj
|