-->
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.  [ 9 posts ] 
Author Message
 Post subject: Update composite keys
PostPosted: Tue Oct 28, 2008 9:18 am 
Newbie

Joined: Sat Oct 25, 2008 1:05 am
Posts: 9
How to update composite keys?

When I update Composite keys,new row get added .rather than updating.

could anyone explain with example?

Hibernate version=3.2


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2008 6:59 pm 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
Please, post mapping file and hibernate code. Anyway, composite-keys used for id's are by default inmuttable. They cannot be changed as they are used to differenciate a record from the others.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2008 12:41 am 
Newbie

Joined: Sat Oct 25, 2008 1:05 am
Posts: 9
Hibernate Mapping 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 Oct 14, 2008 1:17:58 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.esspl.tiss.framework.data.hibernate.orm.Tissecbillcycle" table="TISSECBILLCYCLE" dynamic-update="true" dynamic-insert="true" select-before-update="true">
<composite-id name="id" class="com.esspl.tiss.framework.data.hibernate.orm.TissecbillcycleId" >
<key-property name="ecid" type="int">
<column name="ECID" precision="9" scale="0" />
</key-property>
<key-property name="billcycleid" type="int">
<column name="BILLCYCLEID" precision="9" scale="0" />
</key-property>
<key-property name="startdate" type="date">
<column name="STARTDATE" length="7" />
</key-property>
<key-property name="entrysequenceno" type="int">
<column name="ENTRYSEQUENCENO" precision="9" scale="0" />
</key-property>
</composite-id>
<many-to-one name="tissec" class="com.esspl.tiss.framework.data.hibernate.orm.TISSExternalCarrier" update="false" insert="false" fetch="select">
<column name="ECID" precision="9" scale="0" not-null="true" />
</many-to-one>
<many-to-one name="tissbillcycle" class="com.esspl.tiss.framework.data.hibernate.orm.TissBillCycle" update="false" insert="false" fetch="select">
<column name="BILLCYCLEID" precision="9" scale="0" not-null="true" />
</many-to-one>
<property name="virtualcycleid" type="java.lang.Integer">
<column name="VIRTUALCYCLEID" precision="9" scale="0" />
</property>
<property name="validflag" type="java.lang.Character">
<column name="VALIDFLAG" length="1" />
</property>
<property name="billdirection" type="char">
<column name="BILLDIRECTION" length="1" not-null="true" />
</property>
<property name="enddate" type="date">
<column name="ENDDATE" length="7" />
</property>
<property name="billeduptodate" type="date">
<column name="BILLEDUPTODATE" length="7" />
</property>
<property name="modby" type="string">
<column name="MODBY" length="10" not-null="true" />
</property>
<property name="modon" type="date">
<column name="MODON" length="7" not-null="true" />
</property>
</class>
</hibernate-mapping>

Hibernate Code:

public class Tissecbillcycle implements java.io.Serializable {

/**
* Serial Version UID
*/
private static final long serialVersionUID = -2062161986138331520L;
private TissecbillcycleId id;
private TISSExternalCarrier tissec;
private TissBillCycle tissbillcycle;
private Integer virtualcycleid;
private Character validflag;
private char billdirection;
private Date enddate;
private Date billeduptodate;
private String modby;
private Date modon;

public Tissecbillcycle() {
}

public Tissecbillcycle(TissecbillcycleId id, TISSExternalCarrier tissec,
TissBillCycle tissbillcycle, char billdirection, String modby,
Date modon) {
this.id = id;
this.tissec = tissec;
this.tissbillcycle = tissbillcycle;
this.billdirection = billdirection;
this.modby = modby;
this.modon = modon;
}

public Tissecbillcycle(TissecbillcycleId id, TISSExternalCarrier tissec,
TissBillCycle tissbillcycle, Integer virtualcycleid,
Character validflag, char billdirection, Date enddate,
Date billeduptodate, String modby, Date modon) {
this.id = id;
this.tissec = tissec;
this.tissbillcycle = tissbillcycle;
this.virtualcycleid = virtualcycleid;
this.validflag = validflag;
this.billdirection = billdirection;
this.enddate = enddate;
this.billeduptodate = billeduptodate;
this.modby = modby;
this.modon = modon;
}

public TissecbillcycleId getId() {
return this.id;
}

public void setId(TissecbillcycleId id) {
this.id = id;
}

public TISSExternalCarrier getTissec() {
return this.tissec;
}

public void setTissec(TISSExternalCarrier tissec) {
this.tissec = tissec;
}

public TissBillCycle getTissbillcycle() {
return this.tissbillcycle;
}

public void setTissbillcycle(TissBillCycle tissbillcycle) {
this.tissbillcycle = tissbillcycle;
}

public Integer getVirtualcycleid() {
return this.virtualcycleid;
}

public void setVirtualcycleid(Integer virtualcycleid) {
this.virtualcycleid = virtualcycleid;
}

public Character getValidflag() {
return this.validflag;
}

public void setValidflag(Character validflag) {
this.validflag = validflag;
}

public char getBilldirection() {
return this.billdirection;
}

public void setBilldirection(char billdirection) {
this.billdirection = billdirection;
}

public Date getEnddate() {
return this.enddate;
}

public void setEnddate(Date enddate) {
this.enddate = enddate;
}

public Date getBilleduptodate() {
return this.billeduptodate;
}

public void setBilleduptodate(Date billeduptodate) {
this.billeduptodate = billeduptodate;
}

public String getModby() {
return this.modby;
}

public void setModby(String modby) {
this.modby = modby;
}

public Date getModon() {
return this.modon;
}

public void setModon(Date modon) {
this.modon = modon;
}

}


Id class:
public class TissecbillcycleId implements java.io.Serializable {

/**
* Serial Version UID
*/
private static final long serialVersionUID = -3359696532117839944L;
private int ecid;
private int billcycleid;
private Date startdate;
private int entrysequenceno;

public TissecbillcycleId() {
}

public TissecbillcycleId(int ecid, int billcycleid, Date startdate,
int entrysequenceno) {
this.ecid = ecid;
this.billcycleid = billcycleid;
this.startdate = startdate;
this.entrysequenceno = entrysequenceno;
}

public int getEcid() {
return this.ecid;
}

public void setEcid(int ecid) {
this.ecid = ecid;
}

public int getBillcycleid() {
return this.billcycleid;
}

public void setBillcycleid(int billcycleid) {
this.billcycleid = billcycleid;
}

public Date getStartdate() {
return this.startdate;
}

public void setStartdate(Date startdate) {
this.startdate = startdate;
}

public int getEntrysequenceno() {
return this.entrysequenceno;
}

public void setEntrysequenceno(int entrysequenceno) {
this.entrysequenceno = entrysequenceno;
}

public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof TissecbillcycleId))
return false;
TissecbillcycleId castOther = (TissecbillcycleId) other;

return (this.getEcid() == castOther.getEcid())
&& (this.getBillcycleid() == castOther.getBillcycleid())
&& ((this.getStartdate() == castOther.getStartdate()) || (this
.getStartdate() != null
&& castOther.getStartdate() != null && this
.getStartdate().equals(castOther.getStartdate())))
&& (this.getEntrysequenceno() == castOther.getEntrysequenceno());
}

public int hashCode() {
int result = 17;

result = 37 * result + this.getEcid();
result = 37 * result + this.getBillcycleid();
result = 37 * result
+ (getStartdate() == null ? 0 : this.getStartdate().hashCode());
result = 37 * result + this.getEntrysequenceno();
return result;
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2008 8:36 am 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
Hibernate needs an id that by definition is unmutable, because the id is what uses hibernate to identify and differenciate one entity from other of the same kind. If you want a muttable id, you may use <natural-id>, but additionally, you will need an unmuttable id. Better if it's generated. For example

Code:
<id name="id" type="long" column="ID" />
<natural-id mutable="true">
   <component name="bussinessId" class="com.esspl.tiss.framework.data.hibernate.orm.TissecbillcycleId">
      <property name="ecid" type="int">
         <column name="ECID" precision="9" scale="0" />
      </property>
      <property name="billcycleid" type="int">
         <column name="BILLCYCLEID" precision="9" scale="0" />
      </property>
      <property name="startdate" type="date">
         <column name="STARTDATE" length="7" />
      </property>
      <property name="entrysequenceno" type="int">
         <column name="ENTRYSEQUENCENO" precision="9" scale="0" />
      </property>           
   </component>       
</natural-id>   


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2008 8:39 am 
Beginner
Beginner

Joined: Tue Oct 30, 2007 7:57 am
Posts: 47
Hibernate needs an id that by definition is unmutable, because the id is what uses hibernate to identify and differenciate one entity from other of the same kind. If you want a muttable id, you may use <natural-id>, but additionally, you will need an unmuttable id. Better if it's generated. For example

Code:
<id name="id" type="long" column="ID" />
<natural-id mutable="true">
   <component name="bussinessId" class="com.esspl.tiss.framework.data.hibernate.orm.TissecbillcycleId">
      <property name="ecid" type="int">
         <column name="ECID" precision="9" scale="0" />
      </property>
      <property name="billcycleid" type="int">
         <column name="BILLCYCLEID" precision="9" scale="0" />
      </property>
      <property name="startdate" type="date">
         <column name="STARTDATE" length="7" />
      </property>
      <property name="entrysequenceno" type="int">
         <column name="ENTRYSEQUENCENO" precision="9" scale="0" />
      </property>           
   </component>       
</natural-id>   


Top
 Profile  
 
 Post subject: cannot add one more column in the Table
PostPosted: Wed Oct 29, 2008 9:33 am 
Newbie

Joined: Sat Oct 25, 2008 1:05 am
Posts: 9
cannot add one more column in the table

<id name="id" type="long" column="ID" />

DB:oracle10g


Top
 Profile  
 
 Post subject: cannot add one more column in the Table
PostPosted: Wed Oct 29, 2008 9:35 am 
Newbie

Joined: Sat Oct 25, 2008 1:05 am
Posts: 9
Sorry,cannot add one more column in the table
<id name="id" type="long" column="ID" />
DB:oracle10g

Thanks for your reply.
Is there any other way to solve?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2008 3:45 am 
Newbie

Joined: Thu Oct 30, 2008 3:43 am
Posts: 1
did u try with the mutable id alone?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2008 3:02 pm 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:04 am
Posts: 27
Hibernate does not support changing an object's primary key, as this primary key identifies the database row loaded entities correspond to. Hence changing an object's primary key severs the connection of that loaded entity to the original record in the database, preventing that record from being updated. In particular, if two processes have loaded the same entity, and one process changes his entity's key, the object is "forked", i.e. the two processes now hold different entities, and updates to one entity will not become visible to the other. This unintuitive behavior is why updating keys is strongly discouraged.

However, you can delete an entity and create an entity with similar state, but different key. If this is good enough, you can do:

Code:
session.deleteFlushEvict(entity);
entity.setKey(...);
session.persist(entity);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.