hibernate mapping of subclass and individual fields - java

I have this class hierachy:
class A
{
private String a1;
private Class B a2;
// getter & setter
}
class B
{
private String b1;
private String b2;
// getter & setter
}
class C extends class A
{
private String c1;
// getter and setter for c1;
}
And I need to map C to a single table in a database using hibernate. I try this:
<class name="com.C"
table="myTable" catalog="myCatalog"
polymorphism="implicit">
<property name="a1" type="string">
<column name="column1" length="40" not-null="true" />
</property>
<property name="a2" type="????">
?? B should map to column2 and column3
</property>
<property name="c1" type="string">
<column name="column4" length="40" not-null="true" />
</property>
</class>
How to map field b of class B?
Thanks,

Hi found it using component tag:
<class name="com.C"
table="myTable" catalog="myCatalog"
polymorphism="implicit">
<property name="a1" type="string">
<column name="column1" length="40" not-null="true" />
</property>
<component name="a2" class="com.B">
<property name="b1" type="java.lang.Integer">
<column name="column2" />
</property>
<property name="b2" type="java.lang.Integer">
<column name="column3" />
</property>
</component>
<property name="c1" type="string">
<column name="column4" length="40" not-null="true" />
</property>
</class>

Related

Hibernate query one to many with foreign key pass in the list

Here is my hibernate mapping file:
<class name="com.cms.entity.ContentPartnerMaster" table="BFContentPartnerMaster">
<!-- <cache include="non-lazy" usage="read-only"/> -->
<id name="partnerId" type="int" column="Id">
<generator class="native" />
</id>
<property name="partnerName" column="PartnerName" type="string" />
<property name="partnerDescription" column="PartnerDescription"
type="string" />
<property name="isActive" column="isActive" type="boolean" />
<property name="partnerSalt" column="PartnerSalt" type="string" />
<bag name="carousels" table="BFCaraousal" inverse="true" lazy="true"
fetch="select">
<key>
<column name="Id" not-null="true" />
</key>
<one-to-many class="com.cms.entity.Carousel" />
</bag>
</class>
<class name="com.cms.entity.Carousel" table="BFCaraousal">
<id name="caraousalId" type="int" column="CaraousalId">
<generator class="native" />
</id>
<property name="section" column="Section" type="string" />
<property name="caraousalName" column="CaraousalName" type="string" />
<property name="dateModified" column="DateModified" type="date" />
<property name="dateAdded" column="DateAdded" type="date" />
<property name="addedBy" column="AddedBy" type="int" />
<property name="countryId" column="countryid" type="int" />
<property name="partnerId" column="PartnerId" type="int" />
</class>
Here is my code:
public class ContentPartnerMaster implements Serializable {
private static final long serialVersionUID = 1L;
private int partnerId;
private String partnerName;
private String partnerDescription;
private Boolean isActive;
private String partnerSalt;
private ArrayList<Carousel> carousels;
// getter setter ..
public class Carousel implements Serializable {
private static final long serialVersionUID = 1L;
private Integer caraousalId;
private Integer partnerId;
private Integer countryId;
private String section;
private String caraousalName;
private Integer addedBy;
private Date dateAdded;
private Date dateModified;
Hibernate query one to many with foreign key pass in the list
It is throwing error like id IllegalArgumentException occurred while calling setter of com.cms.entity.ContentPartnerMaster.carousels
Try using Set<> instead of ArrayList.
public class ContentPartnerMaster implements Serializable {
...
private Set<Carousel> carousels;
And later on when you use it, you can cast it to List<>.

What is a proper way to delete a list of objects in Hibernate?

I have an object User, which has a List of UserDictionary. Each UserDictionary has a List of UserWords with cascade delete.
User.java
public class User implements Serializable
{
private Long id;
private String login;
private String password;
private String email;
private Boolean isVerified;
private List<UserDictionary> dictionaries = new ArrayList<>();
private UserTrainingSettings settings;
//getters and setters there
}
And mapping User.hbm.xml
<hibernate-mapping>
<class name="User" dynamic-insert="true" dynamic-update="true" table="USER" entity-name="user">
<id name="id" type="java.lang.Long" column="user_id">
<generator class="native"/>
</id>
<property name="login" type="java.lang.String" length="40" not-null="true" unique="true"/>
<property name="password" type="java.lang.String" length="32" not-null="true"/>
<property name="email" type="java.lang.String" length="100" not-null="true"/>
<property name="isVerified" type="java.lang.Boolean" column="verified" not-null="true"/>
<list name="dictionaries" fetch="join">
<key column="user_id" not-null="true" on-delete="cascade"/>
<one-to-many class="com.github.wordsmemoriser.Model.UserDictionary"/>
</list>
<one-to-one name="settings" class="com.github.wordsmemoriser.Model.UserTrainingSettings"
cascade="javax.persistence.CascadeType.REMOVE"/>
</class>
</hibernate-mapping>
UserDictionary.java
public class UserDictionary
{
private Long id;
private User user;
private String name;
private List<UserWords> words = new ArrayList<>();
//getters and setters there
}
And mapping UserDictionary.hbm.xml
<hibernate-mapping>
<class name="UserDictionary" dynamic-insert="true" dynamic-update="true" table="DICTIONARY">
<id name="id" type="java.lang.Long" column="dict_id">
<generator class="native"/>
</id>
<many-to-one name="user" class="com.github.wordsmemoriser.Model.User" fetch="join">
<column name="user_id" not-null="true"/>
</many-to-one>
<property name="name" type="java.lang.String" length="100"/>
<list name="words">
<key column="dict_id" not-null="true" on-delete="cascade"/>
<one-to-many class="com.github.wordsmemoriser.Model.UserWords"/>
</list>
</class>
</hibernate-mapping>
What is a proper way to delete all UserDictionaries from User? Should I iterate this list, delete every UserDictionary from database, then clear this list and update User like this
session.beginTransaction();
for(UserDictionary dict : user.getDictionaries())
{
session.delete(dict);
}
user.getDictionaries().clear();
session.update(user);
session.getTransaction().commit();
or should I just clear User's list and then update it like this
user.getDictionaries().clear();
session.beginTransaction();
session.update(user);
session.getTransaction().commit();

Row was updated or deleted by another transaction-multithreading

I want to restirict user table updation when two threads are working together, which throws "Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.dmainc.commons.security.User#4]"
. When second threads hits first starts rolling back and throw the exception.
Actually each thread is modifying the passwordAttemptDate.I need to update this field each time when user is logging, how to perform this operation simulaneously for both thread. I am calling this method within a transaction.
ANY SUGGESSION WILL BE APPRECIATED.
public AuthenticationStatus authenticateEncrypted(User user, String password, ISecurityParameters params,
Boolean encryptedPassword)
{
AuthenticationStatus status = new AuthenticationStatus();
status.setUser(user);
status.setStatus(AuthenticationStatus.Status.UNKNOWN_ERROR);
// hash password
String hashedPass = (encryptedPassword == true ? password : hashPassword(password, user.getSalt()));
// compare to stored password and check on attempts if failed
if(hashedPass.equals(user.getPassword()))
{
user.setPasswordAttempts(0);
//THIS IS GETTING UPDATED EACH TIME
user.setPasswordAttemptDate(Calendar.getInstance());
if(getPasswordNotify(user, params) >= 0)
{
status.setStatus(AuthenticationStatus.Status.SUCCESS_NOTIFY);
}
else if(isPasswordExpired(user, params))
{
status.setStatus(AuthenticationStatus.Status.PASSWORD_EXPIRED);
}
else
{
status.setStatus(AuthenticationStatus.Status.SUCCESS);
}
status.setArg(arg);
return status;
}
else
{
user.setPasswordAttempts((user.getPasswordAttempts() == null) ? 1 : user.getPasswordAttempts() + 1);
user.setPasswordAttemptDate(Calendar.getInstance());
status.setStatus(AuthenticationStatus.Status.FAIL);
}
}
catch(Exception pe)
{
if(LOG.isErrorEnabled())
LOG.error(pe, pe);
}
return status;
}
User.hbm.xml
<hibernate-mapping>
<class name="com.dmainc.commons.security.User" table="userdata">
<id name="userId" type="long">
<column name="UserId" />
<generator class="native" />
</id>
<version name="version" type="int">
<column name="Version" not-null="true" />
</version>
<property name="username" type="string">
<column name="Username" length="50" not-null="true" />
</property>
<property name="firstName" type="string">
<column name="FirstName" length="50" />
</property>
<property name="lastName" type="string">
<column name="LastName" length="50" />
</property>
<property name="email" type="string">
<column name="Email" length="50" />
</property>
<property name="status" type="string">
<column name="Status" length="2" />
</property>
<property name="passwordAttempts" type="int">
<column name="PasswordAttempts" />
</property>
<property name="passwordAttemptDate" type="calendar">
<column name="PasswordAttemptDate" length="19" />
<many-to-one name="organization" class="com.dmainc.commons.security.Organization" fetch="select">
<column name="OrganizationId" />
</many-to-one>
<set name="userpasswords" table="userpasswords" inverse="true" lazy="true" fetch="select" cascade="all,delete-orphan">
<key>
<column name="UserId" not-null="true" />
</key>
<one-to-many class="com.dmainc.commons.security.UserPassword" />
</set>
<bag name="userroles" table="userrole" inverse="true" lazy="false" fetch="select" cascade="all,delete-orphan">
<key>
<column name="UserId" not-null="true" />
</key>
<one-to-many class="com.dmainc.commons.security.UserRole" />
</bag>
</class>
Can you try enclosing your code in transaction blocks?
something like:
Session session = null;
Transaction tx = null;
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();
//YOUR CODE HERE
tx.commit();
}catch (Exception ex) {
ex.printStackTrace();
tx.rollback();
}
finally {session.close();}

org.hibernate.property.BasicPropertyAccessor - IllegalArgumentException in class: setter method of property: assessmentBaseId

Stack Trace:
ERROR http-8081-5 org.hibernate.property.BasicPropertyAccessor - IllegalArgumentException in class: org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentBaseData, setter method of property: assessmentBaseId
ERROR http-8081-5 org.hibernate.property.BasicPropertyAccessor - expected type: java.lang.Long, actual value: java.lang.Long
AssessmentBase.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="false">
<class name="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentBaseData"
table="SAM_ASSESSMENTBASE_T">
<id name="assessmentBaseId" column="ID" type="java.lang.Long" unsaved-value="0">
<generator class="native">
<param name="sequence">SAM_ASSESSMENTBASE_ID_S</param>
</generator>
</id>
<discriminator column="isTemplate" />
<property name="parentId" type="long" column="PARENTID" not-null="false" />
<property name="title" type="string" length="255" column="TITLE" not-null="false" />
<property name="description" type="string" length="4000" column="DESCRIPTION" not-null="false" />
<property name="comments" type="string" length="4000" column="COMMENTS" not-null="false" />
<property name="typeId" type="long" column="TYPEID" not-null="false" />
<property name="instructorNotification" type="integer" column="INSTRUCTORNOTIFICATION" not-null="false" />
<property name="testeeNotification" type="integer" column="TESTEENOTIFICATION" not-null="false" />
<property name="multipartAllowed" type="integer" column="MULTIPARTALLOWED" not-null="false" />
<property name="status" type="integer" column="STATUS" not-null="true" />
<property name="createdBy" type="string" length="255" column="CREATEDBY" not-null="true" />
<property name="createdDate" type="timestamp" column="CREATEDDATE" not-null="true" />
<property name="lastModifiedBy" type="string" length="255" column="LASTMODIFIEDBY" not-null="true" />
<property name="lastModifiedDate" type="timestamp" column="LASTMODIFIEDDATE" not-null="true" />
<one-to-one name="assessmentAccessControl"
class="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentAccessControl"
cascade="all" />
<one-to-one name="evaluationModel"
class="org.sakaiproject.tool.assessment.data.dao.assessment.EvaluationModel"
cascade="all" />
<one-to-one name="assessmentFeedback"
class="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentFeedback"
cascade="all" />
<set name="assessmentMetaDataSet" table="SAM_ASSESSMETADATA_T" cascade="all"
inverse="true">
<key column="ASSESSMENTID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentMetaData"/>
</set>
<set name="securedIPAddressSet" table="SAM_SECUREDIP_T" cascade="all"
inverse="true">
<key column="ASSESSMENTID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.SecuredIPAddress"/>
</set>
<subclass name="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentData"
discriminator-value="0">
<property name="assessmentTemplateId" column="ASSESSMENTTEMPLATEID" />
<set name="sectionSet" table="SAM_SECTION_T" cascade="all" order-by="sequence asc"
inverse="true" lazy="true">
<key column="ASSESSMENTID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.SectionData"/>
</set>
<set name="assessmentAttachmentSet" table="SAM_ATTACHMENT_T" cascade="all" order-by="createdDate asc"
inverse="true" lazy="false">
<key column="ASSESSMENTID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentAttachment"/>
</set>
</subclass>
<subclass name="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentTemplateData"
discriminator-value="1" lazy="false" />
</class>
<class name="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentAccessControl"
table="SAM_ASSESSACCESSCONTROL_T">
<id name="id" column="ASSESSMENTID">
<generator class="foreign">
<param name="property">assessmentBase</param>
</generator>
</id>
<one-to-one name="assessmentBase"
class="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentBaseData"
constrained="true" cascade="all" />
<property name="submissionsAllowed" type="integer" column="SUBMISSIONSALLOWED" not-null="false" />
<property name="unlimitedSubmissions" type="boolean" column="UNLIMITEDSUBMISSIONS" not-null="false" />
<property name="submissionsSaved" type="integer" column="SUBMISSIONSSAVED" not-null="false" />
<property name="assessmentFormat" type="integer" column="ASSESSMENTFORMAT" not-null="false" />
<property name="bookMarkingItem" type="integer" column="BOOKMARKINGITEM" not-null="false" />
<property name="timeLimit" type="integer" column="TIMELIMIT" not-null="false" />
<property name="timedAssessment" type="integer" column="TIMEDASSESSMENT" not-null="false" />
<property name="retryAllowed" type="integer" column="RETRYALLOWED" not-null="false" />
<property name="lateHandling" type="integer" column="LATEHANDLING" not-null="false" />
<property name="startDate" type="timestamp" column="STARTDATE" not-null="false" />
<property name="dueDate" type="timestamp" column="DUEDATE" not-null="false" />
<property name="scoreDate" type="timestamp" column="SCOREDATE" not-null="false" />
<property name="feedbackDate" type="timestamp" column="FEEDBACKDATE" not-null="false" />
<property name="retractDate" type="timestamp" column="RETRACTDATE" not-null="false" />
<property name="autoSubmit" type="integer" column="AUTOSUBMIT" not-null="false" />
<property name="itemNavigation" type="integer" column="ITEMNAVIGATION" not-null="false" />
<property name="itemNumbering" type="integer" column="ITEMNUMBERING" not-null="false" />
<property name="submissionMessage" type="string" length="4000" column="SUBMISSIONMESSAGE" not-null="false" />
<property name="releaseTo" type="string" length="255" column="RELEASETO" not-null="false" />
<property name="username" type="string" length="255" column="USERNAME" not-null="false" />
<property name="password" type="string" length="255" column="PASSWORD" not-null="false" />
<property name="finalPageUrl" type="string" length="1023" column="FINALPAGEURL" not-null="false" />
<property name="markForReview" type="integer" column="MARKFORREVIEW" not-null="false" />
</class>
<class name="org.sakaiproject.tool.assessment.data.dao.assessment.EvaluationModel"
table="SAM_ASSESSEVALUATION_T">
<id name="id" column="ASSESSMENTID">
<generator class="foreign">
<param name="property">assessmentBase</param>
</generator>
</id>
<one-to-one name="assessmentBase"
class="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentBaseData"
constrained="true" cascade="all" />
<property name="evaluationComponents" type="string" length="255" column="EVALUATIONCOMPONENTS" not-null="false" />
<property name="scoringType" type="integer" column="SCORINGTYPE" not-null="false" />
<property name="numericModelId" type="string" length="255" column="NUMERICMODELID" not-null="false" />
<property name="fixedTotalScore" type="integer" column="FIXEDTOTALSCORE" not-null="false" />
<property name="gradeAvailable" type="integer" column="GRADEAVAILABLE" not-null="false" />
<property name="isStudentIdPublic" type="integer" column="ISSTUDENTIDPUBLIC" not-null="false" />
<property name="anonymousGrading" type="integer" column="ANONYMOUSGRADING" not-null="false" />
<property name="autoScoring" type="integer" column="AUTOSCORING" not-null="false" />
<property name="toGradeBook" type="string" length="255" column="TOGRADEBOOK" not-null="false" />
</class>
<class name="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentMetaData"
table="SAM_ASSESSMETADATA_T">
<id name="id" column="ASSESSMENTMETADATAID" type="java.lang.Long" unsaved-value="0">
<generator class="native">
<param name="sequence">SAM_ASSESSMETADATA_ID_S</param>
</generator>
</id>
<many-to-one name="assessment" class="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentBaseData"
column="ASSESSMENTID" not-null="true" />
<property name="label" type="string" length="255" column="LABEL" not-null="true" />
<property name="entry" type="string" length="255" column="ENTRY" not-null="false" />
</class>
<class name="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentFeedback"
table="SAM_ASSESSFEEDBACK_T">
<id name="id" column="ASSESSMENTID">
<generator class="foreign">
<param name="property">assessmentBase</param>
</generator>
</id>
<one-to-one name="assessmentBase"
class="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentBaseData"
constrained="true" cascade="all" />
<property name="feedbackDelivery" type="integer" column="FEEDBACKDELIVERY" not-null="false" />
<property name="feedbackAuthoring" type="integer" column="FEEDBACKAUTHORING" not-null="false" />
<property name="editComponents" type="integer" column="EDITCOMPONENTS" not-null="false" />
<property name="showQuestionText" type="boolean" column="SHOWQUESTIONTEXT" not-null="false" />
<property name="showStudentResponse" type="boolean" column="SHOWSTUDENTRESPONSE" not-null="false" />
<property name="showCorrectResponse" type="boolean" column="SHOWCORRECTRESPONSE" not-null="false" />
<property name="showStudentScore" type="boolean" column="SHOWSTUDENTSCORE" not-null="false" />
<property name="showStudentQuestionScore" type="boolean" column="SHOWSTUDENTQUESTIONSCORE" not-null="false" />
<property name="showQuestionLevelFeedback" type="boolean" column="SHOWQUESTIONLEVELFEEDBACK" not-null="false" />
<property name="showSelectionLevelFeedback" type="boolean" column="SHOWSELECTIONLEVELFEEDBACK" not-null="false" />
<property name="showGraderComments" type="boolean" column="SHOWGRADERCOMMENTS" not-null="false" />
<property name="showStatistics" type="boolean" column="SHOWSTATISTICS" not-null="false" />
</class>
<class name="org.sakaiproject.tool.assessment.data.dao.assessment.SectionData" table="SAM_SECTION_T">
<id name="sectionId" column="SECTIONID" type="java.lang.Long" unsaved-value="0">
<generator class="native">
<param name="sequence">SAM_SECTION_ID_S</param>
</generator>
</id>
<many-to-one name="assessment"
class="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentData"
column="ASSESSMENTID" not-null="true" />
<property name="duration" type="integer" column="DURATION" not-null="false" />
<property name="sequence" type="integer" column="SEQUENCE" not-null="false" />
<property name="title" type="string" length="255" column="TITLE" not-null="false" />
<property name="description" type="string" length="4000" column="DESCRIPTION" not-null="false" />
<property name="typeId" type="long" column="TYPEID" not-null="false" />
<property name="status" type="integer" column="STATUS" not-null="true" />
<property name="createdBy" type="string" length="255" column="CREATEDBY" not-null="true" />
<property name="createdDate" type="timestamp" column="CREATEDDATE" not-null="true" />
<property name="lastModifiedBy" type="string" length="255" column="LASTMODIFIEDBY" not-null="true" />
<property name="lastModifiedDate" type="timestamp" column="LASTMODIFIEDDATE" not-null="true" />
<set name="itemSet" table="SAM_ITEM_T" cascade="all" order-by="sequence asc"
inverse="true" lazy="false">
<key column="SECTIONID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.ItemData"/>
</set>
<set name="sectionMetaDataSet" table="SAM_SECTIONMETADATA_T" cascade="all"
inverse="true">
<key column="SECTIONID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.SectionMetaData"/>
</set>
<set name="sectionAttachmentSet" table="SAM_ATTACHMENT_T" cascade="all" order-by="createdDate asc"
inverse="true" lazy="false">
<key column="SECTIONID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.SectionAttachment"/>
</set>
</class>
<class name="org.sakaiproject.tool.assessment.data.dao.assessment.SectionMetaData"
table="SAM_SECTIONMETADATA_T">
<id name="id" column="SECTIONMETADATAID" type="java.lang.Long" unsaved-value="0">
<generator class="native">
<param name="sequence">SAM_SECTIONMETADATA_ID_S</param>
</generator>
</id>
<many-to-one name="section" class="org.sakaiproject.tool.assessment.data.dao.assessment.SectionData"
column="SECTIONID" not-null="true" />
<property name="label" type="string" length="255" column="LABEL" not-null="true" />
<property name="entry" type="string" length="255" column="ENTRY" not-null="false" />
</class>
<class name="org.sakaiproject.tool.assessment.data.dao.assessment.SecuredIPAddress"
table="SAM_SECUREDIP_T">
<id name="id" column="IPADDRESSID" type="java.lang.Long" unsaved-value="0">
<generator class="native">
<param name="sequence">SAM_SECUREDIP_ID_S</param>
</generator>
</id>
<many-to-one name="assessment"
class="org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentBaseData"
column="ASSESSMENTID" not-null="true" />
<property name="hostname" type="string" length="255" column="HOSTNAME" not-null="false" />
<property name="ipAddress" type="string" length="255" column="IPADDRESS" not-null="false" />
</class>
<class name="org.sakaiproject.tool.assessment.data.dao.assessment.AssesmentSummary" table="sam_assessmentgrading_summary">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native">
<param name="sequence">SAM_GRADING_SEQ</param>
</generator>
</id>
<property name="agentId" column="USER_ID" type="java.lang.String"></property>
<property name="assesmentId" column="PUBLISHEDASSESSMENTID" type="java.lang.String"></property>
<property name="score" column="FINAL_SCORE" type="java.lang.Double"></property>
<property name="percentage" column="PERCENTAGE" type="java.lang.String"></property>
<property name="timeSpent" column="TIMEELAPSED" type="java.lang.Long"></property>
<property name="attemptedDate" column="ATTEMPTED_DATE" type="java.util.Date"></property>
<property name="submittedDate" column="SUBMISSION_DATE" type="java.util.Date"></property>
<property name="sectionId" column="SECTION_ID" type="java.lang.Integer"></property>
</class>
<class name="org.sakaiproject.tool.assessment.data.dao.assessment.Lessons"
table="MELETE_LESSON_UNIQUE">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native">
<param name="sequence">LESSON_UNIQUE_SQE</param>
</generator>
</id>
<property name="siteId" column="COURSEID" type="java.lang.String"></property>
<property name="moduleId" column="MODULEID" type="java.lang.Integer"></property>
<property name="sectionId" column="LESSONID" type="java.lang.Integer"></property>
</class>
<class name="org.sakaiproject.tool.assessment.data.dao.assessment.MeleteModule" table="MELETE_MODULE">
<id name="module_Id" column="MODULE_ID" type="java.lang.Integer">
<generator class="native">
<param name="sequence">MELETE_MODULE_SEQ</param>
</generator>
</id>
<one-to-one name="meletecoursemodule"
class="org.sakaiproject.tool.assessment.data.dao.assessment.Melete_Course_Module"
constrained="true" cascade="all" />
<property name="version" column="VERSION" type="java.lang.Integer"></property>
<property name="title" column="TITLE" type="java.lang.String"></property>
<property name="learnObj" column="LEARN_OBJ" type="java.sql.Clob"></property>
<property name="description" column="DESCRIPTION" type="java.sql.Clob"></property>
<property name="keywords" column="KEYWORDS" type="java.lang.String"></property>
<property name="created_by_Fname" column="CREATED_BY_FNAME" type="java.lang.String"></property>
<property name="created_by_Lname" column="CREATED_BY_LNAME" type="java.lang.String"></property>
<property name="userId" column="USER_ID" type="java.lang.String"></property>
<property name="modified_by_Fname" column="MODIFIED_BY_FNAME" type="java.lang.String"></property>
<property name="modified_by_Lname" column="MODIFIED_BY_LNAME" type="java.lang.String"></property>
<property name="institute" column="INSTITUTE" type="java.lang.String"></property>
<property name="whats_Next" column="WHATS_NEXT" type="java.sql.Clob"></property>
<property name="creation_Date" column="CREATION_DATE" type="java.util.Date"></property>
<property name="modification_Date" column="MODIFICATION_DATE" type="java.util.Date"></property>
<property name="seq_Xml" column="SEQ_XML" type="java.sql.Clob"></property>
<property name="courseName" column="COURSENAME" type="java.lang.String"></property>
<property name="courseId" column="COURSEID" type="java.lang.String"></property>
<property name="name" column="NAME" type="java.lang.String"></property>
<property name="lessons" column="LESSONS" type="java.lang.String"></property>
<property name="credits" column="CREDITS" type="java.lang.String"></property>
<property name="content" column="CONTENT" type="java.lang.String"></property>
<property name="rt_Dt" column="RT_DT" type="java.util.Date"></property>
<property name="maxt" column="MAXT" type="java.util.Date"></property>
<property name="course_Level" column="COURSE_LEVEL" type="java.lang.String"></property>
</class>
<class name="org.sakaiproject.tool.assessment.data.dao.assessment.Melete_Course_Module" table="MELETE_COURSE_MODULE">
<id name="module_Id" column="MODULE_ID" type="integer">
<generator class="native">
<param name="sequence">MELETE_COURSE_MODULE_SEQ</param>
</generator>
</id>
<one-to-one name="meletemodule"
class="org.sakaiproject.tool.assessment.data.dao.assessment.MeleteModule"
constrained="true" cascade="all" />
<property name="courseId" column="COURSE_ID" type="string"></property>
<property name="seq_No" column="SEQ_NO" type="integer"></property>
<property name="archv_Flag" column="ARCHV_FLAG" type="boolean"></property>
<property name="date_Archived" column="DATE_ARCHIVED" type="timestamp"></property>
<property name="delete_flag" column="DELETE_FLAG" type="boolean"></property>
</class>
<class name="org.sakaiproject.tool.assessment.data.dao.assessment.Melete_Module_Shdates" table="MELETE_MODULE_SHDATES">
<id name="module_Id" column="MODULE_ID" type="integer">
<generator class="native">
<param name="sequence">MELETE_MODULE_SHDATES_SEQ</param>
</generator>
</id>
<property name="version" column="VERSION" type="integer"></property>
<property name="end_Date" column="END_DATE" type="timestamp"></property>
<property name="start_Date" column="START_DATE" type="timestamp"></property>
</class>
</hibernate-mapping>
AssessmentBaseData.java
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import org.sakaiproject.tool.assessment.data.dao.shared.TypeD;
import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentAccessControlIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentFeedbackIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.EvaluationModelIfc;
import org.sakaiproject.tool.assessment.data.ifc.shared.TypeIfc;
public class AssessmentBaseData
implements java.io.Serializable,AssessmentBaseIfc
{
private Long assessmentBaseId;
private Boolean isTemplate;
private Long parentId;
private String title;
private String description;
private String comments;
private Long typeId;
private Integer instructorNotification;
private Integer testeeNotification;
private Integer multipartAllowed;
private Integer status;
private String createdBy;
private Date createdDate;
private String lastModifiedBy;
private Date lastModifiedDate;
private AssessmentAccessControlIfc assessmentAccessControl;
private EvaluationModelIfc evaluationModel;
private AssessmentFeedbackIfc assessmentFeedback;
private Set assessmentMetaDataSet;
private HashMap assessmentMetaDataMap = new HashMap();
private HashMap assessmentFeedbackMap = new HashMap();
private Set securedIPAddressSet;
public AssessmentBaseData() {}
public AssessmentBaseData(Long assessmentBaseId, String title){
this.assessmentBaseId = assessmentBaseId;
this.title = title;
}
public AssessmentBaseData(Long assessmentBaseId, String title,Date lastModifiedDate){
this.assessmentBaseId = assessmentBaseId;
this.title = title;
this.lastModifiedDate = lastModifiedDate;
}
public AssessmentBaseData(Long assessmentBaseId, String title,Date lastModifiedDate, String lastModifiedBy){
this.assessmentBaseId = assessmentBaseId;
this.title = title;
this.lastModifiedDate = lastModifiedDate;
this.lastModifiedBy = lastModifiedBy;
}
public AssessmentBaseData(Long assessmentBaseId, String title,Date lastModifiedDate, Long typeId){
this.assessmentBaseId = assessmentBaseId;
this.title = title;
this.lastModifiedDate = lastModifiedDate;
this.typeId = typeId;
}
public AssessmentBaseData(Boolean isTemplate, Long parentId,
String title, String description, String comments,
Long typeId,
Integer instructorNotification, Integer testeeNotification,
Integer multipartAllowed, Integer status, String createdBy,
Date createdDate, String lastModifiedBy,
Date lastModifiedDate) {
this.isTemplate = isTemplate;
this.parentId = parentId;
this.title = title;
this.description = description;
this.comments = comments;
this.typeId = typeId;
this.instructorNotification = instructorNotification;
this.testeeNotification = testeeNotification;
this.multipartAllowed = multipartAllowed;
this.status = status;
this.createdBy = createdBy;
this.createdDate = createdDate;
this.lastModifiedBy = lastModifiedBy;
this.lastModifiedDate = lastModifiedDate;
}
public Long getAssessmentBaseId() {
return this.assessmentBaseId;
}
public void setAssessmentBaseId(Long assessmentBaseId) {
this.assessmentBaseId = assessmentBaseId;
}
public Boolean getIsTemplate() {
return this.isTemplate;
}
public void setIsTemplate(Boolean isTemplate) {
this.isTemplate = isTemplate;
}
public Long getParentId() {
return this.parentId;
}
public void setParentId(Long parentId) {
this.parentId = parentId;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public String getComments() {
return this.comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public Integer getInstructorNotification() {
return this.instructorNotification;
}
public void setInstructorNotification(Integer instructorNotification) {
this.instructorNotification = instructorNotification;
}
public Integer getTesteeNotification() {
return this.testeeNotification;
}
public void setTesteeNotification(Integer testeeNotification) {
this.testeeNotification = testeeNotification;
}
public Integer getMultipartAllowed() {
return this.multipartAllowed;
}
public void setMultipartAllowed(Integer multipartAllowed) {
this.multipartAllowed = multipartAllowed;
}
public Long getTypeId() {
return this.typeId;
}
public void setTypeId(Long typeId) {
this.typeId = typeId;
}
public Integer getStatus() {
return this.status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getCreatedBy() {
return this.createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public Date getCreatedDate() {
return this.createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
}
In this loop when i am trying to save object in Session which makes that Exception
List pendingAssessList=service.getpendingassessmentData(pendingassessIds,syncDate,siteid);
for(int i=0;i<pendingAssessList.size();i++)
{
AssessmentData ad=(AssessmentData)pendingAssessList.get(i);
AssessmentData new_a = afq.prepareAssessment(ad, ServerConfigurationService.getServerUrl());
openSession.save(new_a);
}
assessmentdata
public class AssessmentData extends org.sakaiproject.tool.assessment.data.dao.assessment.AssessmentBaseData
implements java.io.Serializable, AssessmentIfc
{
}

How to fetch data from one table & insert into another using hibernate mapping

I'm working on online exam project by using Struts Spring and Hibernate integration with mysql & Eclipse kepler.
While submitting the values in registration.jsp page, i'm trying to store that values in two different tables (user_details,address) within the same database. I can able to store them in DB, but i can't able to fetch the user_id which is a foreign key for address table. user_id is the primary key in user_details table.Except user_id in address table, all the other fields are filled with the correct values. I'm trying to use that in address table. But i can't do that. I have attached the code that i'm using right now,
user.hbm.xml
<hibernate-mapping>
<class name="UserDetails" table="user_details">
<id name="user_id" type="int" column="user_id" >
<generator class="identity">
</generator>
</id>
<property name="first_name" type="string">
<column name="first_name"/>
</property>
<property name="last_name" type="string">
<column name="last_name"/>
</property>
<property name="email" type="string">
<column name="email"/>
</property>
<property name="password" type="string">
<column name="password"/>
</property>
<property name="gender" type="string">
<column name="gender"/>
</property>
<property name="dob" type="int">
<column name="dob"/>
</property>
<property name="phone" type="int">
<column name="phone"/>
</property>
<property name="experience" type="float">
<column name="experience"/>
</property>
<set name="addr" table="address"
inverse="true" lazy="true" fetch="select" cascade = "save-update">
<key>
<column name="user_id" not-null="false" />
</key>
<one-to-many class="UserAddress" />
</set>
</class>
</hibernate-mapping>
useraddress.hbm.xml
<hibernate-mapping>
<class name="UserAddress" table="address">
<id name="address_id" type="int" column="address_id">
<generator class="identity"/>
</id>
<property name="addr_line1" type="string">
<column name="addr_line_1"/>
</property>
<property name="addr_line2" type="string">
<column name="addr_line_2"/>
</property>
<property name="addr_line3" type="string">
<column name="addr_line_3"/>
</property>
<property name="city" type="string">
<column name="city"/>
</property>
<property name="zipcode" type="int">
<column name="zipcode"/>
</property>
<property name="state" type="string">
<column name="state"/>
</property>
<property name="country" type="string">
<column name="country"/>
</property>
<many-to-one name="user_detail" class="UserDetails" fetch="select">
<column name="user_id" not-null="false"></column>
</many-to-one>
</class>
</hibernate-mapping>
UserDetails.java
public class UserDetails {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
//#OneToMany (mappedBy="user_details", cascade = CascadeType.ALL)
#OneToMany (cascade = { CascadeType.PERSIST, CascadeType.MERGE}, mappedBy="user_detail")
public int user_id; //primary key
private String first_name;
private String last_name;
private String email;
private String password;
private String gender;
private int dob;
private int phone;
private float experience;
private Set<UserAddress> addr;//set name
//getters and setters created
UserAddress.java
public class UserAddress {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int address_id; //primary key
#ManyToOne(fetch=FetchType.EAGER, targetEntity=UserDetails.class)
#JoinColumn(name="user_id")
private UserDetails user_detail;
private String addr_line1;
private String addr_line2;
private String addr_line3;
private String city;
private int zipcode;
private String state;
private String country;
//getters and setters created
I think i'm missing something in hibernate mapping part, because i can able to store other address table values except user_id. If anyone is interested to work with the complete code

Categories