java - Implementing Custom hibernate type -


i trying implement hibernate mapping sample schema (order entry) provided oracle default installation orderentry schema has several table 1 of them customer table having few columns customtyped columns example custom type created following statement ;

create or replace type "cust_address_typ"  object ( street_address     varchar2(40) , postal_code        varchar2(10) , city               varchar2(30) , state_province     varchar2(10) , country_id         char(2) ); 

and customer table created below statement

create table oe.customers  (   customer_id number (6)  not null ,   cust_first_name varchar2 (20 byte)   constraint cust_fname_nn not null ,   cust_last_name varchar2 (20 byte)   constraint cust_lname_nn not null ,   cust_address oe.cust_address_typ ,   ) 

i came know need implement usertype interface here when try retrieve giving error

 org.hibernate.mappingexception: property mapping has wrong number of columns: com.dto.oe.customers.cust_addressdata type: com.dto.oe.custaddresstype             @ org.hibernate.mapping.persistentclass.validate(persistentclass.java:497) @ org.hibernate.mapping.rootclass.validate(rootclass.java:270) @ org.hibernate.cfg.configuration.validate(configuration.java:1360) @ org.hibernate.cfg.configuration.buildsessionfactory(configuration.java:1851) @ org.hibernate.cfg.configuration.buildsessionfactory(configuration.java:1930) @ com.test.connection.hibernateutil.getsession(hibernateutil.java:82) @ com.test.connection.hibernateutil.getcustomer(hibernateutil.java:70) @ com.test.connection.hibernateutil.main(hibernateutil.java:18) 

wheras customtype object pojo class cust_addressdata , class implemented usertype interface custaddresstype

    import java.io.serializable;     import java.sql.preparedstatement;     import java.sql.resultset;     import java.sql.resultsetmetadata;     import java.sql.sqlexception;     import java.sql.types;      import org.hibernate.hibernateexception;     import org.hibernate.engine.spi.sessionimplementor;     import org.hibernate.usertype.usertype;      public class custaddresstype implements usertype {     /**      * returns object 2 level cache      */     public object assemble(serializable cached, object owner)             throws hibernateexception {         return cached;     }      /**      * used create snapshots of object      */     public object deepcopy(object value) throws hibernateexception {         final cust_addressdata recievedparam = (cust_addressdata) value;         final cust_addressdata addressdata = new cust_addressdata(recievedparam);         return addressdata;     }      /**      * method called when hibernate puts data in second level cache.      * data stored in serializable form      */     public serializable disassemble(final object value)             throws hibernateexception {         return (serializable) value;     }      public boolean equals(final object o1, final object o2)             throws hibernateexception {         boolean isequal = false;         if (o1 == o2) {             isequal = true;         }         if (null == o1 || null == o2) {             isequal = false;         } else {             isequal = o1.equals(o2);         }         return isequal;         // work correctly equals()         // method must implemented correctly cust_addressdata class     }      public int hashcode(final object arg0) throws hibernateexception {         return arg0.hashcode();     }      public boolean ismutable() {         return true;     }      public object nullsafeget(final resultset resultset, final string[] names,             sessionimplementor sessionimp, final object owner)             throws hibernateexception, sqlexception {          // owner here class call retrieve data made.         // in case test class          cust_addressdata addresssdata = new cust_addressdata();         // order of columns given sqltypes() method         if (!resultset.wasnull()) {               final string street_address = resultset.getstring(names[0]);             final string postal_code = resultset.getstring(names[1]);             final string city = resultset.getstring(names[2]);             final string state_province = resultset.getstring(names[3]);             final string country_id = resultset.getstring(names[4]);              addresssdata.setcity(city);             addresssdata.setcountry_id(country_id);             addresssdata.setpostal_code(postal_code);             addresssdata.setstate_province(state_province);             addresssdata.setstreet_address(street_address);             system.out.println("street_address "+street_address +"  names  "+names[0]);         } else {             system.err.println("resultset null in custaddresstype");         }         return addresssdata;     }      public void nullsafeset(final preparedstatement statement, final object value, final int index,             sessionimplementor arg3) throws hibernateexception, sqlexception {         if (null == value) {             statement.setnull(index, types.varchar);             statement.setnull(index + 1, types.varchar);             statement.setnull(index + 2, types.varchar);             statement.setnull(index + 3,types.varchar);             statement.setnull(index + 4,types.varchar);         } else {             cust_addressdata addressdata = (cust_addressdata) value;              if (null != addressdata.getstreet_address()) {                 string  street_address = new string(addressdata.getstreet_address());                 statement.setstring(index , street_address);             } else {                 statement.setnull(index , types.varchar);             }              if (null != addressdata.getpostal_code()) {                  string  postal_code = new string(addressdata.getpostal_code());                  statement.setstring(index+1 , postal_code);             } else {                 statement.setnull(index +1, types.varchar);             }              if (null != addressdata.getcity()) {              string  city = new string(addressdata.getcity());                 statement.setstring(index+2 , city);            } else {                statement.setnull(index +2, types.varchar);            }              if (null != addressdata.getstate_province()) {              string  postal_code = new string(addressdata.getstate_province());                 statement.setstring(index+3 , postal_code);            } else {                statement.setnull(index +3, types.varchar);            }               if (null != addressdata.getcountry_id()) {              string  postal_code = new string(addressdata.getcountry_id());                 statement.setstring(index+4 , postal_code);            } else {                statement.setnull(index +4, types.varchar);            }          }      }      public object replace(final object original, final object target,             final object owner) throws hibernateexception {         return this.deepcopy(original);     }      @suppresswarnings("rawtypes")     public class returnedclass() {         return cust_addressdata.class;     }      public int[] sqltypes() {         return new int[] { types.varchar, types.varchar, types.varchar,                 types.varchar, types.varchar };     } } 
     import java.io.serializable;      import javax.persistence.column;      import javax.persistence.entity;      import javax.persistence.table;     @suppresswarnings("serial")     @entity     @table(name="cust_address_typ")     public class cust_addressdata implements serializable {     /**      *       */ private static final long serialversionuid = 1l;     @column(name = "street_address")     private string street_address;     @column(name = "postal_code")     private string postal_code;     @column(name = "city")     private string city;     @column(name = "state_province")     private string state_province;     @column(name = "country_id")     private string country_id;      public cust_addressdata() {     }      public cust_addressdata(cust_addressdata other) {         this.setcity(other.getcity());         this.setcountry_id(other.getcountry_id());         this.setpostal_code(other.getpostal_code());         this.setstreet_address(other.getstreet_address());         this.setstate_province(other.getstate_province());      }      public string getstreet_address() {         return street_address;     }      public void setstreet_address(string street_address) {         this.street_address = street_address;     }      public string getpostal_code() {         return postal_code;     }      public void setpostal_code(string postal_code) {         this.postal_code = postal_code;     }      public string getcity() {         return city;     }      public void setcity(string city) {         this.city = city;     }      public string getstate_province() {         return state_province;     }      public void setstate_province(string state_province) {         this.state_province = state_province;     }      public string getcountry_id() {         return country_id;     }      public void setcountry_id(string country_id) {         this.country_id = country_id;     }      @override     public boolean equals(object obj) {          boolean isequal = false;         if (obj instanceof cust_addressdata) {             cust_addressdata addressdata = (cust_addressdata) obj;              isequal = addressdata.getcity().equals(this.getcity())                     && addressdata.getstate_province().equals(this.getstate_province())                     && addressdata.getcountry_id().equals(this.getcountry_id())                     && addressdata.getpostal_code().equals(this.getpostal_code())                     && addressdata.getstreet_address().equals(this.getstreet_address())                     ;         }         return isequal;     }      @override     public int hashcode() {         int hash = this.getcountry_id().hashcode();         hash = hash * 17 + this.getpostal_code().hashcode();         hash = hash * 31 + this.getstreet_address().hashcode();         hash = hash * 13 + this.getstate_province().hashcode();         hash = hash * 14 + this.getcity().hashcode();         return hash;     }      @override     public string tostring() {          return "[ " + this.getclass() + " { city : " + city                 + ", street_address: " + street_address + ", postal_code: "                 + postal_code + ", state_province: " + state_province + ""                         + ",country_id :"+country_id+"}]";     } } 

full sources:

one more observation sqltypes method in custaddresstype class

       public int[] sqltypes() {              return new int[] { types.varchar, types.varchar, types.varchar,                     types.varchar, types.varchar };         } 

is 1 returning number of columns if make return 1 column i'm getting column value null.

not sure goin wrong,any suggestion doing wrong or clue appreciated

using struct class in custaddresstype class has resolved issue source code custaddresstype class follows.

 import java.io.serializable;  import java.sql.connection;  import java.sql.preparedstatement;  import java.sql.resultset;  import java.sql.sqlexception;  import java.sql.struct;  import java.sql.types;   import oracle.sql.struct;  import oracle.sql.structdescriptor;   import org.hibernate.hibernateexception;   import org.hibernate.engine.spi.sessionimplementor;   import org.hibernate.usertype.usertype;   public class custaddresstype implements usertype {     private static final int sql_type = types.struct;     private static final string object_type = "cust_address_typ";     /**      * returns object 2 level cache     */     public object assemble(serializable cached, object owner)         throws hibernateexception {     return cached; }  /**  * used create snapshots of object  */ public object deepcopy(object value) throws hibernateexception {      if (value == null) {          return null;      }     final cust_addressdata recievedparam = (cust_addressdata) value;     final cust_addressdata addressdata = new cust_addressdata(recievedparam);      return addressdata; }  /**  * method called when hibernate puts data in second level cache.  * data stored in serializable form  */ public serializable disassemble(final object value)         throws hibernateexception {     return (serializable) value; }  public boolean equals(final object o1, final object o2)         throws hibernateexception {     boolean isequal = false;     if (o1 == o2) {         isequal = true;     }     if (null == o1 || null == o2) {         isequal = false;     } else {         isequal = o1.equals(o2);     }     return isequal;     // work correctly equals()     // method must implemented correctly cust_addressdata class }  public int hashcode(final object arg0) throws hibernateexception {     return arg0.hashcode(); }  public boolean ismutable() {     return true; }  public object nullsafeget(final resultset resultset, final string[] names,         sessionimplementor sessionimp, final object owner)         throws hibernateexception, sqlexception {      // owner here class call retrieve data made.     // in case test class      final cust_addressdata addresssdata = new cust_addressdata();      final struct struct = (struct) resultset.getobject(names[0]);      if (resultset.wasnull()) {          return null;      }     addresssdata.setcity((string)struct.getattributes()[0]);     addresssdata.setcountry_id((string)struct.getattributes()[1]);     addresssdata.setpostal_code((string)struct.getattributes()[2]);     addresssdata.setstate_province((string)struct.getattributes()[3]);     addresssdata.setstreet_address((string)struct.getattributes()[4]);      return addresssdata; }  public void nullsafeset(final preparedstatement statement, final object value, final int index,         sessionimplementor arg3) throws hibernateexception, sqlexception {     if (value == null) {          statement.setnull(index, sql_type, object_type);      }      else      {         final cust_addressdata addresssdata = (cust_addressdata) value;         final object[] values = new object[] { addresssdata.getcity(),addresssdata.getcountry_id(), addresssdata.getpostal_code(),                 addresssdata.getstate_province(),addresssdata.getstreet_address() };         final connection connection = statement.getconnection();         final struct struct = new struct(structdescriptor.createdescriptor( object_type,connection), connection, values);         statement.setobject(index, struct, sql_type);      }  }  public object replace(final object original, final object target,         final object owner) throws hibernateexception {     return this.deepcopy(original); }  @suppresswarnings("rawtypes") public class returnedclass() {     return cust_addressdata.class; }  public int[] sqltypes() {     return new int[] {sql_type}; } 

}

thanks @stevechambers pointing out,


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -