2

I am quite new to Hibernate world. I have modeled by means of ER CASE TOOL (TOAD) my database and I have defined several User Data Type (Composite Type). For example assume I have a type Contact declared as follow in PostgreSQL

CREATE TYPE Contact AS
( "email" Varchar,
"phone" Varchar,
"mobile" Varchar,
"other" Varchar );

Now assume i use it on Users Entity like in the following SQL code

CREATE TABLE Users(
idUser Serial NOT NULL,
login Character varying(20) NOT NULL,
password Character varying(20) NOT NULL,
name Character varying(30),
surname Character varying(50),
contact Contact
)  

-- Add keys for table Users

ALTER TABLE Users ADD CONSTRAINT pkIdUser PRIMARY KEY (idUser)
;

I have configured and used Hibernate Tools to do a reverse engineering of ER model, but I have problem on the definition of the Contact user type because it is mapped as Serializable and not as a custom type, What I am trying to have is to define a Contact Hibernate UserType and change according the reverse.xml file of the Hibernate Tools.....but when I try to use my implementation of the UserType I obtain this message:

"org.hibernate.cfg.JDBCBinderException: The type it.mypackage.FullContactInfoType found on Table: users column: fullcontactinfo spans multiple columns. Only single column types allowed. The type it.mypackage.FullContactInfoType found on Table: users column: fullcontactinfo spans multiple columns. Only single column types allowed. "

Have you some link on a simple example on how to achieve this?. Until now I was not been to able to map the user data type...I am considering to remove the User Data Type from the ER model and explode the fields of wich it is composed.

I am using Hibernate 3.5.4

Regards and thanks in advance for your support.

LU RD
  • 34,438
  • 5
  • 88
  • 296
quonn
  • 51
  • 1
  • 4

1 Answers1

1

Let me know if this solves the issue. Here is an example of postgres/hibernate implementation http://www.hibernatespatial.org/tutorial.html

or the other info piece that might be useful for this situation is:

Am assuming that you have declared your Java objects with the entity annotation. Create a ContactUsetype object extending Hibernate Usertype object.

public Object nullSafeGet(ResultSet resultSet, String[] email, String[] phone, String[] mobile, String[] other, Object owner)
throws HibernateException, SQLException {
assert email.length == 1;
assert phone.length == 1;
assert mobile.length==1;
assert other.length==1

if (resultSet.wasNull()) {
return null;
}
final Contact contactVariable = new Contact(resultSet.getObject(email[0]).toString()
                                       ,resultSet.getObject(phone[0]).toString(),
                                        resultSet.getObject(mobile[0]).toString(),
                                        resultSet.getObject(other[0]).toString());
return contactVariable;
}

public void nullSafeSet(PreparedStatement statement, Object value, int index)
throws HibernateException, SQLException {
statement.setObject(index, value);
}
}

Finally when you create users object

Users user= new User(..., new Contact ("(some@email.com,123-456-4566,111-11-1111,aim)")
sathish_at_madison
  • 823
  • 11
  • 34
  • Hi Sathish thank you for you answer, I am already playing with UserType but I am not succeding to do it, when I declare on the reveng.xml configuration file the mapping with my user Type I obtain an org.hibernate.cfg.JDBCBinderException as I have written on my post. I am trying to do a reverse engineering of what I have already modeled by means of ER....however I will look further into your suggestion and try again this solution....just a question the signature of nullSafeGet is (ResultSet resultSet, String[] names, Object owner) and not the one you report...or Am I missing something?? Regards – quonn Mar 15 '12 at 08:09
  • You are correct nullSafeGet, did not realize that I created an array for each object, thanks. One other verification, if you happened to look at the mapping at http://www.hibernatespatial.org/tutorial.html link? there is a sample mapping file that might be useful to peer review. Cheers. – sathish_at_madison Mar 15 '12 at 14:34
  • Actually I have developed a RowEconder that transform any object graphs to the Row format used by postgresql to define a Structure, I am succeding in letting it bind with a PGObject in my UserType but when I look on the generated SQL code it wrongs because add ' where it hasn't to do....it is using internally when I give a PGObject setStringParameter method....so this is the result 'ROW(ROW(''Italy'',''Country'',''Via Test'',345,''00155''),ROW(''pippo@pippo.it'',null,null,null))' instead of ROW(ROW('Italy','Country','Via Andrea Noale',345,'00155'),ROW('pippo@pippo.it',null,null,null))'.... – quonn Mar 15 '12 at 18:20
  • looking further it seems that the org.postgresql.jdbc2.AbstractJdbc2Statement statement class doesn't support internally the Types.STRUCT....so anyone knows if there is a replacement jdbc drivers who support it? – quonn Mar 15 '12 at 18:27