dursun
dursun

Reputation: 1846

Hibernate with NVARCHAR2

I have table with a column as NVARCHAR2, and I have build my object with hibernate annotations, When i want to insert into DB or fetch from DB the result is something like that "???????", I have implemented a custom Dialect as follows but it did not work.

public class CustomOracleDialect extends Oracle10gDialect{

    public CMSCustomOracleDialect() {
        registerHibernateType( Types.NVARCHAR, Hibernate.STRING.getName() );
        registerColumnType( Types.VARCHAR, "nvarchar2($1)" );
        registerColumnType( Types.CLOB, "nclob" );
        registerColumnType( Types.NCLOB, "nclob" );
    }

}

Upvotes: 3

Views: 8813

Answers (3)

robothy
robothy

Reputation: 1320

public class CustomOracleDialect extends Oracle10gDialect {

    public CustomOracleDialect() {
        super();
        registerColumnType(Types.NVARCHAR, "nvarchar2($l)");//$l not $1
        registerHibernateType(Types.NVARCHAR, StandardBasicTypes.STRING.getName());
    }
}

Then set CustomOracleDialect to your hibernate configuration file.

Upvotes: 0

Sean F
Sean F

Reputation: 2390

Might be a little late but I have just come across this issue aswell and I found that can annotate your hibernate class with

@Nationalized

or

@Type(type="org.hibernate.type.StringNVarcharType")

which makes your element use org.hibernate.type.StringNVarcharType instead of the VarChar type string

which was introduced into hibernate in release 4.1.10 and above. So that it correctly uses the NVarChar2 type or else it will default back to VarChar2 which then looses your international characters.

Hibernate Jira where this was resolved: https://hibernate.atlassian.net/browse/HHH-5869

And Jboss-Hibernate Documentation: https://docs.jboss.org/hibernate/orm/4.3/devguide/en-US/html/ch08.html#value-national-character-types

Upvotes: 5

Andy Dufresne
Andy Dufresne

Reputation: 6190

How are you mapping your columns in the object? The below mappings works

@Column(name = "PHONE_NUMBER", nullable = false, columnDefinition = "nvarchar2 (2000)")
public String getPhoneNumber() {
    return this.phoneNumber;
}

Note this is JPA @Column annotation. Calling entityManager.persist() on the phone object works.

Upvotes: 3

Related Questions