-->
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: Hibernate, Postgres, and "inet" columns
PostPosted: Tue Feb 26, 2008 8:23 am 
Newbie

Joined: Tue Feb 26, 2008 7:40 am
Posts: 1
Hello all.

I am fairly new to Hibernate, and am trying to map an existing Postgres DB with Hibernate.

One of my tables is set up with a "inet" field, which doesn't have an equivalent in Hibernate.

What's the best practice for dealing with this?
I found some code submitted where someone had written an Inet4AddressType.java - however, upon using it, I get other errors:
Initial SessionFactory creation failed.org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111

Basically, I'd like the community's advice about the best way to approach this sort of problem, and are there any documents about this, existing code I can use, to save re-inventing and re-writing the wheel.

Using PG 8.0.15, the latest Hibernate-core from the site, and Sun JDK 6.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 7:05 am 
Newbie

Joined: Thu Aug 07, 2008 6:58 am
Posts: 2
do you find a solution ? i have the same trouble and not yet solve it


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2008 5:44 pm 
Newbie

Joined: Thu Nov 06, 2008 5:41 pm
Posts: 1
I would also like to see a solution for this.

Can anyone please assist?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 12:19 pm 
Newbie

Joined: Wed Aug 27, 2008 5:51 am
Posts: 1
Has anyone come up with a solution?


Top
 Profile  
 
 Post subject: Re: Hibernate, Postgres, and "inet" columns
PostPosted: Tue Jan 26, 2010 2:35 pm 
Newbie

Joined: Fri Jan 22, 2010 5:29 am
Posts: 1
Location: Poland
You must use custom value types
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-types-custom

Code:
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.HibernateException;


public class InetType implements org.hibernate.usertype.UserType{

    private static final int[] SQL_TYPES = {Types.OTHER};

    @Override
    public int[] sqlTypes() {
        return SQL_TYPES;
    }

    @Override
    public Class returnedClass() {
        return String.class;
    }

    @Override
    public boolean equals(Object arg0, Object arg1) throws HibernateException {
        return arg0 == arg1;
    }

    @Override
    public int hashCode(Object arg0) throws HibernateException {
        return arg0.hashCode();
    }

    @Override
    public Object nullSafeGet(ResultSet arg0, String[] arg1, Object arg2) throws HibernateException, SQLException {
        String grade = arg0.getString(arg1[0]);
        return arg0.wasNull() ? null : grade;
    }

    @Override
        public void nullSafeSet(PreparedStatement aPreparedStatement, Object aObject, int aint) throws HibernateException, SQLException {
         if (aObject == null){
            aPreparedStatement.setNull(aint, Types.OTHER );
        }else{
            aPreparedStatement.setObject(aint, aObject, Types.OTHER );
        }
    }

    @Override
    public Object deepCopy(Object arg0) throws HibernateException {
        return arg0;
    }

    @Override
    public boolean isMutable() {
        return false;
    }

    @Override
    public Serializable disassemble(Object value) throws HibernateException {
        return (Serializable) value;
    }

    @Override
    public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
        return arg0;
    }

    @Override
    public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
        return arg0;
    }
}


and property

Code:
<property name="ipAdres" type="InetType" >
            <column name="ipadres" sql-type="inet"  />
</property>


postgresql driver must be 8.3 or later
ipAdres in java class is String


Top
 Profile  
 
 Post subject: Re: Hibernate, Postgres, and "inet" columns
PostPosted: Sat Mar 27, 2010 11:52 am 
Beginner
Beginner

Joined: Sat Mar 27, 2010 11:03 am
Posts: 27
Location: Berlin
thx to Doppelganger !!

i ran into the same problem but using hibernate annotations.

here ist the solution:

1. Import the InetType class
2. annotate the Entityclass with the Typedefinition like this (2nd row is important):
Code:
@Entity
@TypeDef(name="inet", typeClass = InetType.class)
@Table(name = "ipaddresses", schema = "public", uniqueConstraints = @UniqueConstraint(columnNames = {"ip", "device_id" }))
public class Ipaddresses extends JavaBeansSupport implements java.io.Serializable { ...

}


3. annotate the the Getter of the column like this (2nd row is important):
Code:
   
@Column(name = "ip",  nullable = false)
@Type(type = "inet")
public String getIp() {
return this.ip;
}


And everything works fine !

I hope anybody help this information.

Moe


Top
 Profile  
 
 Post subject: Re: Hibernate, Postgres, and "inet" columns
PostPosted: Sat Jun 05, 2010 5:28 pm 
Newbie

Joined: Sat Jun 05, 2010 5:26 pm
Posts: 1
Guys, thank you very much. I've run into similar problem but with your help I managed to tackle this one. Thanks again!!!!


Top
 Profile  
 
 Post subject: Re: Hibernate, Postgres, and "inet" columns
PostPosted: Thu Feb 10, 2011 6:02 pm 
Newbie

Joined: Thu Feb 10, 2011 5:38 pm
Posts: 1
I'm having the same problem and I've tried to fix it with the solution above.

My environment:
Hibernate 3 (using annotations)
Spring 3
Java 1.6
Tomcat 6

The only real environmental difference to the ones listed above is Spring, but I don't think that is the issue. In Spring, I have setup my hibernate dialect to be org.hibernate.dialect.PostgreSQLDialect.

I used the annotations solution listed but am getting the following stack trace:

Caused by: org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111
at org.hibernate.dialect.TypeNames.get(TypeNames.java:79)
at org.hibernate.dialect.TypeNames.get(TypeNames.java:104)
at org.hibernate.dialect.Dialect.getTypeName(Dialect.java:347)
at org.hibernate.mapping.Column.getSqlType(Column.java:208)
at org.hibernate.mapping.Table.validateColumns(Table.java:280)
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1174)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:139)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:389)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:855)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:774)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
... 75 more


Top
 Profile  
 
 Post subject: Re: Hibernate, Postgres, and "inet" columns
PostPosted: Fri Feb 11, 2011 2:26 am 
Beginner
Beginner

Joined: Sat Mar 27, 2010 11:03 am
Posts: 27
Location: Berlin
Hi,

it seems your dialect mapping doesn´t work...

Quote:
Caused by: org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111


that should be your entry point to look for the problem.

regards moe


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.