-->
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.  [ 7 posts ] 
Author Message
 Post subject: Problem with Column Type: Wrong datatype for read
PostPosted: Tue Sep 14, 2004 9:22 am 
Newbie

Joined: Tue Sep 14, 2004 12:05 am
Posts: 5
I'm running into a problem where it appears that a column that I intend to map as a String is being read in as a Byte. Apologies in advance if I've provided too much information below, let me know the right amount and I'll adjust in the future. (sorry, accidentally posted to tools).

I've read: http://forum.hibernate.org/viewtopic.ph ... ht=getbyte but didn't get an idea of a fix.

Hibernate version: 2.1.6

Mapping documents:

Code:
   <?xml version="1.0" encoding="UTF-8"?>
   <!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.iss.va.vo.impl.SecurityIndex"
      table="securityindex"
      dynamic-update="false"
      dynamic-insert="false"
      select-before-update="false"
       >
      <composite-id
          name="comp_id"
          class="com.iss.va.vo.impl.SecurityIndexPK"
      >
              <key-property
            name="securityId"
            type="java.lang.String"
            column="SecurityID"
            length="14"
         />
              <key-property
            name="securityTypeid"
            type="java.lang.Byte"
            column="SecurityTypeid"
            length="4"
         />
              <key-property
            name="indexTypeId"
            type="java.lang.Short"
            column="IndexTypeID"
            length="6"
         />
      </composite-id>
      <property
          name="companyName"
          type="java.lang.String"
          update="true"
          insert="true"
          access="property"
          column="CompanyName"
          length="60"
      />
      <property
          name="ticker"
          type="java.lang.String"
          update="true"
          insert="true"
          access="property"
          column="Ticker"
          length="20"
      />
      <property
          name="startDate"
          type="java.util.Date"
          update="true"
          insert="true"
          access="property"
          column="StartDate"
          length="19"
      />
      <property
          name="endDate"
          type="java.util.Date"
          update="true"
          insert="true"
          access="property"
          column="EndDate"
          length="19"
      />
      <many-to-one
          name="securitytypecode"
          class="com.iss.va.vo.impl.SecurityTypeCode"
          cascade="none"
          outer-join="false"
          update="false"
          insert="false"
          access="property"
      >
          <column
         name="SecurityTypeid"
          />
      </many-to-one>
      <many-to-one
          name="security"
          class="com.iss.va.vo.impl.Security"
          cascade="none"
          outer-join="false"
          update="false"
          insert="false"
          access="property"
      >
          <column
         name="SecurityTypeid"
          />
          <column
         name="SecurityID"
          />
      </many-to-one>
      <many-to-one
          name="company"
          class="com.iss.va.vo.impl.Company"
          cascade="none"
          outer-join="false"
          update="true"
          insert="true"
          access="property"
      >
          <column
         name="CompanyID"
          />
      </many-to-one>
       </class>
   </hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
   Session s = DAO.createSession();
   SecurityIndexPK pk2 = new SecurityIndexPK();
   pk2.setSecurityId("001204106");
   pk2.setSecurityTypeid( new Byte((byte)1));
   pk2.setIndexTypeId(new Short((short)6));
   Class c = com.iss.va.vo.impl.SecurityIndex.class;
   SecurityIndex securityIndex = (com.iss.va.vo.SecurityIndex) s.load(c, (com.iss.va.vo.impl.SecurityIndexPK)pk2);
   System.out.println("securityIndex=\n"+securityIndex);


Full stack trace of any exception that occurs:

Code:
   [WARN] JDBCExceptionReporter - SQL Error: 0, SQLState: S1009
   [ERROR] JDBCExceptionReporter - Value '001204106' is out of range [-127,127]
   [WARN] JDBCExceptionReporter - SQL Error: 0, SQLState: S1009
   [ERROR] JDBCExceptionReporter - Value '001204106' is out of range [-127,127]
   [ERROR] JDBCExceptionReporter - could not load: [com.iss.va.vo.impl.SecurityIndex#com.iss.va.vo.impl.SecurityIndexPK@63642865[securityId=001204106,securityTypeid=1,indexTypeId=6]] <java.sql.SQLException: Value '001204106' is out of range [-127,127]>java.sql.SQLException: Value '001204106' is out of range [-127,127]
      at com.mysql.jdbc.ResultSet.getByte(ResultSet.java:701)
      at com.mysql.jdbc.ResultSet.getByte(ResultSet.java:720)
      at net.sf.hibernate.type.ByteType.get(ByteType.java:18)
      at net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:62)
      at net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:53)
      at net.sf.hibernate.type.AbstractType.hydrate(AbstractType.java:66)
      at net.sf.hibernate.type.ComponentType.hydrate(ComponentType.java:387)
      at net.sf.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:145)
      at net.sf.hibernate.type.ManyToOneType.hydrate(ManyToOneType.java:61)
      at net.sf.hibernate.loader.Loader.hydrate(Loader.java:686)
      at net.sf.hibernate.loader.Loader.loadFromResultSet(Loader.java:627)
      at net.sf.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:586)
      at net.sf.hibernate.loader.Loader.getRow(Loader.java:501)
      at net.sf.hibernate.loader.Loader.getRowFromResultSet(Loader.java:213)
      at net.sf.hibernate.loader.Loader.doQuery(Loader.java:281)
      at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
      at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:911)
      at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:931)
      at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
      at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
      at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
      at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2117)
      at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1991)
      at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1920)
      at Foo.fooB(Foo.java:50)
      at Foo.main(Foo.java:41)


Name and version of the database you are using:

mysqld-nt.exe Ver 4.0.20a-nt-log for NT on i32
mysql-connector-java-3.1.14

The generated SQL (show_sql=true):

Code:
  select securityin0_.SecurityID as SecurityID0_, securityin0_.SecurityTypeid as Security2_0_, securityin0_.IndexTypeID as IndexTyp3_0_, securityin0_.CompanyName as CompanyN4_0_, securityin0_.Ticker as Ticker0_, securityin0_.StartDate as StartDate0_, securityin0_.EndDate as EndDate0_, securityin0_.SecurityTypeid as Security2_0_, securityin0_.SecurityID as SecurityID0_, securityin0_.CompanyID as CompanyID0_ from securityindex securityin0_ where securityin0_.SecurityID=? and securityin0_.SecurityTypeid=? and securityin0_.IndexTypeID=?


Debug level Hibernate log excerpt:

debug

DB Info:

Code:
  mysql> describe securityindex;
   +----------------+-------------+------+-----+---------------------+-------+
   | Field          | Type        | Null | Key | Default             | Extra |
   +----------------+-------------+------+-----+---------------------+-------+
   | SecurityID     | varchar(14) |      | PRI |                     |       |
   | SecurityTypeid | tinyint(4)  |      | PRI | 0                   |       |
   | IndexTypeID    | smallint(6) |      | PRI | 0                   |       |
   | CompanyName    | varchar(60) |      |     |                     |       |
   | Ticker         | varchar(20) | YES  |     | NULL                |       |
   | StartDate      | datetime    |      |     | 0000-00-00 00:00:00 |       |
   | EndDate        | datetime    | YES  |     | NULL                |       |
   | CompanyID      | int(11)     | YES  | MUL | NULL                |       |
   +----------------+-------------+------+-----+---------------------+-------+
   8 rows in set (0.00 sec)
   
   

   --
   -- Table structure for table `securityindex`
   --
   CREATE TABLE securityindex (
     SecurityID varchar(14) NOT NULL default '',
     SecurityTypeid tinyint(4) NOT NULL default '0',
     IndexTypeID smallint(6) NOT NULL default '0',
     CompanyName varchar(60) NOT NULL default '',
     Ticker varchar(20) default NULL,
     StartDate datetime NOT NULL default '0000-00-00 00:00:00',
     EndDate datetime default NULL,
     CompanyID int(11) default NULL,
     PRIMARY KEY  (SecurityID,SecurityTypeid,IndexTypeID),
     KEY IDX_SecurityID2 (SecurityID),
     KEY IDX_SecurityTypeID2 (SecurityTypeid),
     KEY IDX_SecurityIndex_CompanyID_SecurityTypeID2 (CompanyID,SecurityTypeid),
     KEY hind_1794105432_8A2 (CompanyID),
     KEY hind_c_1794105432_3A2 (IndexTypeID),
     CONSTRAINT `SecurityIndex2Company2` FOREIGN KEY (`CompanyID`) REFERENCES `company` (`CompanyID`),
     CONSTRAINT `SecurityIndex2Security2` FOREIGN KEY (`SecurityTypeid`, `SecurityID`) REFERENCES `secu
   rity` (`SecurityTypeID`, `SecurityID`),
     CONSTRAINT `SecurityIndex2SecurityTypeID2` FOREIGN KEY (`SecurityTypeid`) REFERENCES `securitytype
   code` (`SecurityTypeID`)
   ) TYPE=InnoDB;


A sample query:

Code:
mysql> select * from securityindex securityin0_ where securityin0_.SecurityID="001204106" and securityin0_.SecurityTypeid=1 and securityin0_.IndexType
ID=6;
+------------+----------------+-------------+--------------------+--------+---------------------+---------+-----------+
| SecurityID | SecurityTypeid | IndexTypeID | CompanyName | Ticker | StartDate | EndDate | CompanyID |
+------------+----------------+-------------+--------------------+--------+---------------------+---------+-----------+
| 001204106 | 1 | 6 | FOO Resources Inc. | FOO | 2004-02-25 10:31:00 | NULL | 316 |
+------------+----------------+-------------+--------------------+--------+---------------------+---------+-----------+
1 row in set (0.02 sec)


Java Class & PK:

Code:
import java.io.Serializable;
import java.util.Date;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
/**
* @hibernate.class
* table="securityindex"
*
*/
public class SecurityIndex extends ValueObjectImpl implements Serializable, com.iss.va.vo.SecurityIndex {

/** identifier field */
private com.iss.va.vo.impl.SecurityIndexPK comp_id;

/** persistent field */
private String companyName;

/** nullable persistent field */
private String ticker;

/** persistent field */
private Date startDate;

/** nullable persistent field */
private Date endDate;

/** nullable persistent field */
private com.iss.va.vo.impl.SecurityTypeCode securitytypecode;

/** nullable persistent field */
private com.iss.va.vo.impl.Security security;

/** persistent field */
private com.iss.va.vo.impl.Company company;

/** full constructor */
public SecurityIndex(com.iss.va.vo.impl.SecurityIndexPK comp_id, String companyName, String ticker, Date startDate, Date endDate, com.iss.va.vo.impl.SecurityTypeCode securitytypecode, com.iss.va.vo.impl.Security security, com.iss.va.vo.impl.Company company) {
this.comp_id = comp_id;
this.companyName = companyName;
this.ticker = ticker;
this.startDate = startDate;
this.endDate = endDate;
this.securitytypecode = securitytypecode;
this.security = security;
this.company = company;
}

/** default constructor */
public SecurityIndex() {
}

/** minimal constructor */
public SecurityIndex(com.iss.va.vo.impl.SecurityIndexPK comp_id, String companyName, Date startDate, com.iss.va.vo.impl.Company company) {
this.comp_id = comp_id;
this.companyName = companyName;
this.startDate = startDate;
this.company = company;
}

/**
* @hibernate.id
* generator-class="assigned"
*
*/
public com.iss.va.vo.impl.SecurityIndexPK getComp_id() {
return this.comp_id;
}

public void setComp_id(com.iss.va.vo.impl.SecurityIndexPK comp_id) {
this.comp_id = comp_id;
}
public com.iss.va.vo.SecurityIndexPK getPK() {
return (com.iss.va.vo.SecurityIndexPK)getComp_id();
}
public void setPK(com.iss.va.vo.SecurityIndexPK pk) {
setComp_id((com.iss.va.vo.impl.SecurityIndexPK)pk);
}

/**
* @hibernate.property
* column="CompanyName"
* length="60"
* not-null="true"
*
*/
public String getCompanyName() {
return this.companyName;
}

public void setCompanyName(String companyName) {
this.companyName = companyName;
}

/**
* @hibernate.property
* column="Ticker"
* length="20"
*
*/
public String getTicker() {
return this.ticker;
}

public void setTicker(String ticker) {
this.ticker = ticker;
}

/**
* @hibernate.property
* column="StartDate"
* length="19"
* not-null="true"
*
*/
public Date getStartDate() {
return this.startDate;
}

public void setStartDate(Date startDate) {
this.startDate = startDate;
}

/**
* @hibernate.property
* column="EndDate"
* length="19"
*
*/
public Date getEndDate() {
return this.endDate;
}

public void setEndDate(Date endDate) {
this.endDate = endDate;
}

/**
* @hibernate.many-to-one
* update="false"
* insert="false"
* outer-join="false"
*
* @hibernate.column
* name="SecurityTypeid"
*
*/
public com.iss.va.vo.impl.SecurityTypeCode getSecuritytypecode() {
return this.securitytypecode;
}

public void setSecuritytypecode(com.iss.va.vo.impl.SecurityTypeCode securitytypecode) {
this.securitytypecode = securitytypecode;
}

/**
* @hibernate.many-to-one
* update="false"
* insert="false"
* outer-join="false"
*
* @hibernate.column
* name="SecurityTypeid"
*
* @hibernate.column
* name="SecurityID"
*
*/
public com.iss.va.vo.impl.Security getSecurity() {
return this.security;
}

public void setSecurity(com.iss.va.vo.impl.Security security) {
this.security = security;
}

/**
* @hibernate.many-to-one
* not-null="true"
* outer-join="false"
* @hibernate.column name="CompanyID"
*
*/
public com.iss.va.vo.impl.Company getCompany() {
return this.company;
}

public void setCompany(com.iss.va.vo.impl.Company company) {
this.company = company;
}

public boolean equals(Object other) {
if ( !(other instanceof SecurityIndex) ) return false;
SecurityIndex castOther = (SecurityIndex) other;
return new EqualsBuilder()
.append(this.getComp_id(), castOther.getComp_id())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getComp_id())
.toHashCode();
}

}

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/** @author Hibernate CodeGenerator */
public class SecurityIndexPK implements Serializable, com.iss.va.vo.SecurityIndexPK {

/** identifier field */
private String securityId;

/** identifier field */
private Byte securityTypeid;

/** identifier field */
private Short indexTypeId;

/** full constructor */
public SecurityIndexPK(String securityId, Byte securityTypeid, Short indexTypeId) {
this.securityId = securityId;
this.securityTypeid = securityTypeid;
this.indexTypeId = indexTypeId;
}

/** default constructor */
public SecurityIndexPK() {
}

/**
* @hibernate.property
* column="SecurityID"
* length="14"
*
*/
public String getSecurityId() {
return this.securityId;
}

public void setSecurityId(String securityId) {
this.securityId = securityId;
}

/**
* @hibernate.property
* column="SecurityTypeid"
* length="4"
*
*/
public Byte getSecurityTypeid() {
return this.securityTypeid;
}

public void setSecurityTypeid(Byte securityTypeid) {
this.securityTypeid = securityTypeid;
}

/**
* @hibernate.property
* column="IndexTypeID"
* length="6"
*
*/
public Short getIndexTypeId() {
return this.indexTypeId;
}

public void setIndexTypeId(Short indexTypeId) {
this.indexTypeId = indexTypeId;
}

public String toString() {
return new ToStringBuilder(this)
.append("securityId", getSecurityId())
.append("securityTypeid", getSecurityTypeid())
.append("indexTypeId", getIndexTypeId())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof SecurityIndexPK) ) return false;
SecurityIndexPK castOther = (SecurityIndexPK) other;
return new EqualsBuilder()
.append(this.getSecurityId(), castOther.getSecurityId())
.append(this.getSecurityTypeid(), castOther.getSecurityTypeid())
.append(this.getIndexTypeId(), castOther.getIndexTypeId())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getSecurityId())
.append(getSecurityTypeid())
.append(getIndexTypeId())
.toHashCode();
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 6:29 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Don't post your issues in more than one area of the forum... that's the best way to hear from Gavin... and he won't probably give you any answer ;)

Can't help you much with this issue but there is something that doesn't look clear to me in your mapping: it seems you have two fields mapped on the 'SecurityID' column:

- the first from the "SecurityIndex" composite key
- the second from the "security" many-to-one collection

Right ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 14, 2004 10:24 pm 
Newbie

Joined: Tue Sep 14, 2004 12:05 am
Posts: 5
Hi Bertrand:

I think your talking about the mapping of a field for the composit key and its use in a foreign key. Is there a better way to do this?:

Composit Key:

Code:
        <composite-id
            name="comp_id"
            class="com.iss.va.vo.impl.SecurityIndexPK"
        >
                     <key-property
                        name="securityId"
                        type="java.lang.String"
                        column="SecurityID"
                        length="14"
                />

                     <key-property
                        name="securityTypeid"
                        type="java.lang.Byte"
                        column="SecurityTypeid"
                        length="4"
                />

                     <key-property
                        name="indexTypeId"
                        type="java.lang.Short"
                        column="IndexTypeID"
                        length="6"
                />

        </composite-id>


Foreign Key:

Code:
        <many-to-one
            name="security"
            class="com.iss.va.vo.impl.Security"
            cascade="none"
            outer-join="false"
            update="false"
            insert="false"
            access="property"
        >
            <column
                name="SecurityTypeid"
            />
            <column
                name="SecurityID"
            />
        </many-to-one>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 15, 2004 3:33 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
To be honest, I've never played with composite id - but if I understand correctly your mapping, linking your SecurityIndex instance to another Security instance will affect your primary key (because some of the fields are shared).

Doesn't look safe to me... and I don't know how Hibernate behaves in such case. Can't help you more than this - sorry.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 15, 2004 8:33 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
This works fine if you set update and insert to flase for the foreign key mapping, as sick has done correctly.

Problems really start, if only a part of the FK is part of the PK, because then you have to play with extra properties and adjust them in an Interceptor. AFAIK Hibernate 3 solves this with the new forumla atribute for columns.

HTH
Ernst


Top
 Profile  
 
 Post subject: Do I fit the problem situation
PostPosted: Wed Sep 15, 2004 2:04 pm 
Newbie

Joined: Tue Sep 14, 2004 12:05 am
Posts: 5
Ernst,

Do I meet the problem criteria of: "only a part of the FK is part of the PK"? The two FK relationships with fields in the PK have all of the fields in the FK as part of the PK.

I'm going to try stripping the object of it's FK's/relationships and adding them back one at a time. I'll also have a peek at the source this evening.

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 16, 2004 2:46 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Hi sick

No you don't meet the criteria. The intention of my post was to answer betrtands question, whether it's legal to do a mapping like that.

To be (hopefully) more clear:
The problems only shows up, if some columns of the FK in your many-to-one mapping are part of the PK and some are not. Then you have to set update and insert to false, leaving you with the problem, that the non PK columns are never set.
AFAIK in Hibernate 3 this can be solved by using the formula instead of the column attribute for the PK columns. With Hibernate 2 the only way I found is to add an additional property for the non PK columns and adjust them in an Interceptor.

HTH
Ernst


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