Hibernate with NVARCHAR2

两盒软妹~` 提交于 2019-12-24 03:55:11

问题


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" );
    }

}

回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/27720775/hibernate-with-nvarchar2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!