Hibernate将东方而不是拉丁符号写入SQL Server

问题描述 投票:0回答:1

我使用JBOSS Hibernate工具对MS SQL Server进行反向工程以获取我的数据模型对象。

然后我用Java编写了一个小型测试程序来打开一个会话,使用配置,实例化一个Customer对象,将其持久化到数据库中。

该示例运行将一条记录写入数据库并关闭会话。

奇怪的是,我之前从未见过的是将“东方”字符保存到数据库列中。

数据库:

Windows 2012 R2 Standard上的SQL Server(Express)10.50.1600 列customerCode和customerName是nvarchar,null Java App:Eclipse Neon,Maven,Hibernate 4.3,Java 1.8

注意:我是Hibernate的新手。

还有什么我可以尝试或重构?

我试过的东西用IBM数据工作室做一个SQL插件(成功)

没有成功

  1. 在连接URL中添加UTF-8
  2. 在hibernate xml中添加UTF-8
  3. 创建一个SqlServerDialectWithNvarchar类并将其输入到hibernate xml中。

公共类SqlServerDialectWithNvarchar扩展org.hibernate.dialect.SQLServerDialect {

public SqlServerDialectWithNvarchar(){
    registerHibernateType(Types.NVARCHAR, 4000, "string");
    registerColumnType(Types.BIGINT, "bigint");
    registerColumnType(Types.BIT, "bit");
    registerColumnType(Types.CHAR, "nchar(1)");
    registerColumnType(Types.VARCHAR, 4000, "nvarchar($l)");
    registerColumnType(Types.VARCHAR, "nvarchar(max)");
    registerColumnType(Types.VARBINARY, 4000, "varbinary($1)");
    registerColumnType(Types.VARBINARY, "varbinary(max)");
    registerColumnType(Types.BLOB, "varbinary(max)");
    registerColumnType(Types.CLOB, "nvarchar(max)");
}

}

主程序:

public static void main(String[] args) {
    String btnName = "OrderTrack.java ";

    System.out.println(btnName + " started.");
    System.out.println(btnName + " hibernate.enh-tas.cfg.xml");

    Configuration cfg1 = new Configuration();
    cfg1.configure("/hibernate.enh-tas.cfg.xml");

    SessionFactory sf1 = cfg1.configure().buildSessionFactory();

    System.out.println(btnName + " open hibernate session.");
    Session session = sf1.openSession();
    session.beginTransaction();

    TblCustomers customer = new TblCustomers();
    customer.setCustomerName("TEST XXX");
    customer.setCustomerCode("DELETE ME");

    session.persist(customer);
    session.getTransaction().commit();

    System.out.println(btnName + " closing hibernate session.");
    session.close();
    System.out.println(btnName + " session closed.");

}

主程序运行后使用IBM Data Studio进行SELECT查询

 customerId     customerCode    customerName
 ----------     --------------  ---------------
         30     Ԁt䐉䱅呅⁅䕍        Ԁt合卅⁔塘X      

Hibernate XML

hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.useUnicode">true</property>
        <property name="hibernate.connection.characterEncoding">utf-8</property>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="hibernate.connection.password">pwd</property>
        <property name="hibernate.connection.url">jdbc:sqlserver://serverhost:1433;characterEncoding=UTF-8;</property>
        <property name="hibernate.connection.username">tms</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="hibernate.encrypt">false</property>
        <property name="hibernate.search.autoregister_listeners">true</property>
        <property name="hibernate.show_sql">false</property>
        <property name="hibernate.validator.apply_to_ddl">false</property>

客户类/ ** *由hbm2java生成的TblCustomers * /

@Entity
@Table(name = "tblCustomers", schema = "dbo", catalog = "TAS", uniqueConstraints = @UniqueConstraint(columnNames = {
        "customerCode", "customerContact" }))
public class TblCustomers implements java.io.Serializable {

    private Integer customerId;
    private TblFrequency tblFrequency;
    private Boolean customerLocked;
    private Boolean customerDeleted;
    private Serializable customerCode;
    private Serializable customerName;
    private Boolean customerIsOperator;
    private Boolean customerIsSupplier;
    private Boolean customerIsOwner;
    private Boolean customerIsDrawer;
    private Serializable customerContact;
    private Serializable customerContactPosition;
    private Serializable customerAddress1;
    private Serializable customerAddress2;
    private Serializable customerTown;
    private Serializable customerCounty;
    private Serializable customerPostCode;
    private Serializable customerCountry;
    private Serializable customerTelNo;
    private Serializable customerFaxNo;
    private Serializable customerEmail1;
    private Serializable customerEmail2;
    private Serializable customerEmail3;
    private Serializable customerEmail4;
    private Serializable customerEmail5;
    private Serializable customerInvoiceName;
    private Serializable customerInvoiceAddress1;
    private Serializable customerInvoiceAddress2;
    private Serializable customerInvoiceTown;
    private Serializable customerInvoiceCounty;
    private Serializable customerInvoicePostCode;
    private Serializable customerInvoiceCountry;
    private Serializable customerDefermentAccountNo;
    private Double customerBond;
    private Double customerBondTrigger;
    private Double customerBondRemaining;
    private Serializable customerNotes;
    private Integer customerCustomerGroupId;
    private Serializable customerRdcocode;
    private Serializable customerVatregNo;
    private Boolean customerIsHoldingAccount;
    private Set<TblContacts> tblContactses = new HashSet(0);
    private Set<TblMovementCustomers> tblMovementCustomerses = new HashSet(0);
    private Set<TblTas2orderConfig> tblTas2orderConfigsForTas2ocCarrierNumber = new HashSet(0);
    private Set<TblJournalCustomers> tblJournalCustomerses = new HashSet(0);
    private Set<TblTas2orderConfig> tblTas2orderConfigsForTas2ocCustomerNumber = new HashSet(0);
    private Set<TblSites> tblSiteses = new HashSet(0);
    private Set<TblWomovements> tblWomovementses = new HashSet(0);
    private Set<TblTas2destinations> tblTas2destinationses = new HashSet(0);
    private Set<TblTas2orderConfig> tblTas2orderConfigsForTas2ocSupplierNumber = new HashSet(0);
    private Set<TblTas2orderProduct> tblTas2orderProducts = new HashSet(0);
    private Set<TblCustomerStocks> tblCustomerStockses = new HashSet(0);

    public TblCustomers() {
    }

    public TblCustomers(TblFrequency tblFrequency, Boolean customerLocked, Boolean customerDeleted,
            Serializable customerCode, Serializable customerName, Boolean customerIsOperator,
            Boolean customerIsSupplier, Boolean customerIsOwner, Boolean customerIsDrawer, Serializable customerContact,
            Serializable customerContactPosition, Serializable customerAddress1, Serializable customerAddress2,
            Serializable customerTown, Serializable customerCounty, Serializable customerPostCode,
            Serializable customerCountry, Serializable customerTelNo, Serializable customerFaxNo,
            Serializable customerEmail1, Serializable customerEmail2, Serializable customerEmail3,
            Serializable customerEmail4, Serializable customerEmail5, Serializable customerInvoiceName,
            Serializable customerInvoiceAddress1, Serializable customerInvoiceAddress2,
            Serializable customerInvoiceTown, Serializable customerInvoiceCounty, Serializable customerInvoicePostCode,
            Serializable customerInvoiceCountry, Serializable customerDefermentAccountNo, Double customerBond,
            Double customerBondTrigger, Double customerBondRemaining, Serializable customerNotes,
            Integer customerCustomerGroupId, Serializable customerRdcocode, Serializable customerVatregNo,
            Boolean customerIsHoldingAccount, Set tblContactses, Set tblMovementCustomerses,
            Set tblTas2orderConfigsForTas2ocCarrierNumber, Set tblJournalCustomerses,
            Set tblTas2orderConfigsForTas2ocCustomerNumber, Set tblSiteses, Set tblWomovementses,
            Set tblTas2destinationses, Set tblTas2orderConfigsForTas2ocSupplierNumber, Set tblTas2orderProducts,
            Set tblCustomerStockses) {
        this.tblFrequency = tblFrequency;
        this.customerLocked = customerLocked;
        this.customerDeleted = customerDeleted;
        this.customerCode = customerCode;
        this.customerName = customerName;
        this.customerIsOperator = customerIsOperator;
        this.customerIsSupplier = customerIsSupplier;
        this.customerIsOwner = customerIsOwner;
        this.customerIsDrawer = customerIsDrawer;
        this.customerContact = customerContact;
        this.customerContactPosition = customerContactPosition;
        this.customerAddress1 = customerAddress1;
        this.customerAddress2 = customerAddress2;
        this.customerTown = customerTown;
        this.customerCounty = customerCounty;
        this.customerPostCode = customerPostCode;
        this.customerCountry = customerCountry;
        this.customerTelNo = customerTelNo;
        this.customerFaxNo = customerFaxNo;
        this.customerEmail1 = customerEmail1;
        this.customerEmail2 = customerEmail2;
        this.customerEmail3 = customerEmail3;
        this.customerEmail4 = customerEmail4;
        this.customerEmail5 = customerEmail5;
        this.customerInvoiceName = customerInvoiceName;
        this.customerInvoiceAddress1 = customerInvoiceAddress1;
        this.customerInvoiceAddress2 = customerInvoiceAddress2;
        this.customerInvoiceTown = customerInvoiceTown;
        this.customerInvoiceCounty = customerInvoiceCounty;
        this.customerInvoicePostCode = customerInvoicePostCode;
        this.customerInvoiceCountry = customerInvoiceCountry;
        this.customerDefermentAccountNo = customerDefermentAccountNo;
        this.customerBond = customerBond;
        this.customerBondTrigger = customerBondTrigger;
        this.customerBondRemaining = customerBondRemaining;
        this.customerNotes = customerNotes;
        this.customerCustomerGroupId = customerCustomerGroupId;
        this.customerRdcocode = customerRdcocode;
        this.customerVatregNo = customerVatregNo;
        this.customerIsHoldingAccount = customerIsHoldingAccount;
        this.tblContactses = tblContactses;
        this.tblMovementCustomerses = tblMovementCustomerses;
        this.tblTas2orderConfigsForTas2ocCarrierNumber = tblTas2orderConfigsForTas2ocCarrierNumber;
        this.tblJournalCustomerses = tblJournalCustomerses;
        this.tblTas2orderConfigsForTas2ocCustomerNumber = tblTas2orderConfigsForTas2ocCustomerNumber;
        this.tblSiteses = tblSiteses;
        this.tblWomovementses = tblWomovementses;
        this.tblTas2destinationses = tblTas2destinationses;
        this.tblTas2orderConfigsForTas2ocSupplierNumber = tblTas2orderConfigsForTas2ocSupplierNumber;
        this.tblTas2orderProducts = tblTas2orderProducts;
        this.tblCustomerStockses = tblCustomerStockses;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)

    @Column(name = "customerId", unique = true, nullable = false)
    public Integer getCustomerId() {
        return this.customerId;
    }

    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }

    @Column(name = "customerCode")
    public Serializable getCustomerCode() {
        return this.customerCode;
    }

    public void setCustomerCode(Serializable customerCode) {
        this.customerCode = customerCode;
    }

    @Column(name = "customerName")
    public Serializable getCustomerName() {
        return this.customerName;
    }

    public void setCustomerName(Serializable customerName) {
        this.customerName = customerName;
    }

控制台输出(hibernate.show_sql = true)

    DEBUG - begin
    DEBUG - Obtaining JDBC connection
    DEBUG - Obtained JDBC connection
    DEBUG - initial autocommit status: false
    DEBUG - Executing identity-insert immediately
    DEBUG - insert into TAS.dbo.tblCustomers (customerAddress1, customerAddress2, customerBond, customerBondRemaining, customerBondTrigger, customerCode, customerContact, customerContactPosition, customerCountry, customerCounty, customer_customerGroupId, customerDefermentAccountNo, customerDeleted, customerEmail1, customerEmail2, customerEmail3, customerEmail4, customerEmail5, customerFaxNo, customerInvoiceAddress1, customerInvoiceAddress2, customerInvoiceCountry, customerInvoiceCounty, customerInvoiceName, customerInvoicePostCode, customerInvoiceTown, customerIsDrawer, customerIsHoldingAccount, customerIsOperator, customerIsOwner, customerIsSupplier, customerLocked, customerName, customerNotes, customerPostCode, customerRDCOCode, customerTelNo, customerTown, customerVATRegNo, customerFrequencyId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    Hibernate: insert into TAS.dbo.tblCustomers (customerAddress1, customerAddress2, customerBond, customerBondRemaining, customerBondTrigger, customerCode, customerContact, customerContactPosition, customerCountry, customerCounty, customer_customerGroupId, customerDefermentAccountNo, customerDeleted, customerEmail1, customerEmail2, customerEmail3, customerEmail4, customerEmail5, customerFaxNo, customerInvoiceAddress1, customerInvoiceAddress2, customerInvoiceCountry, customerInvoiceCounty, customerInvoiceName, customerInvoicePostCode, customerInvoiceTown, customerIsDrawer, customerIsHoldingAccount, customerIsOperator, customerIsOwner, customerIsSupplier, customerLocked, customerName, customerNotes, customerPostCode, customerRDCOCode, customerTelNo, customerTown, customerVATRegNo, customerFrequencyId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    DEBUG - Natively generated identity: 37
    DEBUG - committing
    DEBUG - Processing flush-time cascades
    DEBUG - Dirty checking collections
    DEBUG - Collection found: [com.xpagesbeast.data.entities.enh.TblCustomers.tblContactses#37], was: [<unreferenced>] (initialized)
    DEBUG - Collection found: [com.xpagesbeast.data.entities.enh.TblCustomers.tblCustomerStockses#37], was: [<unreferenced>] (initialized)
    DEBUG - Collection found: [com.xpagesbeast.data.entities.enh.TblCustomers.tblJournalCustomerses#37], was: [<unreferenced>] (initialized)
    DEBUG - Collection found: [com.xpagesbeast.data.entities.enh.TblCustomers.tblMovementCustomerses#37], was: [<unreferenced>] (initialized)
    DEBUG - Collection found: [com.xpagesbeast.data.entities.enh.TblCustomers.tblSiteses#37], was: [<unreferenced>] (initialized)
    DEBUG - Collection found: [com.xpagesbeast.data.entities.enh.TblCustomers.tblTas2destinationses#37], was: [<unreferenced>] (initialized)
    DEBUG - Collection found: [com.xpagesbeast.data.entities.enh.TblCustomers.tblTas2orderConfigsForTas2ocCarrierNumber#37], was: [<unreferenced>] (initialized)
    DEBUG - Collection found: [com.xpagesbeast.data.entities.enh.TblCustomers.tblTas2orderConfigsForTas2ocCustomerNumber#37], was: [<unreferenced>] (initialized)
    DEBUG - Collection found: [com.xpagesbeast.data.entities.enh.TblCustomers.tblTas2orderConfigsForTas2ocSupplierNumber#37], was: [<unreferenced>] (initialized)
    DEBUG - Collection found: [com.xpagesbeast.data.entities.enh.TblCustomers.tblTas2orderProducts#37], was: [<unreferenced>] (initialized)
    DEBUG - Collection found: [com.xpagesbeast.data.entities.enh.TblCustomers.tblWomovementses#37], was: [<unreferenced>] (initialized)
    DEBUG - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
    DEBUG - Flushed: 11 (re)creations, 0 updates, 0 removals to 11 collections
    DEBUG - Listing entities:
    DEBUG - com.xpagesbeast.data.entities.enh.TblCustomers{customerInvoicePostCode=null, customerContactPosition=null, customerDefermentAccountNo=null, tblCustomerStockses=[], tblTas2destinationses=[], customerIsDrawer=null, customerInvoiceCountry=null, tblContactses=[], customerCustomerGroupId=null, tblWomovementses=[], 

customerName=TEST XXX, customerDeleted=null,customerBondTrigger=null, customerTown=null, customerVatregNo=null, tblMovementCustomerses=[], tblSiteses=[], tblTas2orderConfigsForTas2ocCarrierNumber=[], customerInvoiceName=null, 

customerCode=DELETE ME, customerEmail1=null, customerNotes=null, customerEmail3=null, customerEmail2=null, customerCountry=null, customerEmail5=null, customerInvoiceAddress1=null, tblTas2orderConfigsForTas2ocCustomerNumber=[], customerEmail4=null, customerInvoiceAddress2=null, customerIsOwner=null, tblTas2orderProducts=[], customerBondRemaining=null, customerFaxNo=null, customerIsSupplier=null, customerContact=null, customerPostCode=null, customerId=37, customerCounty=null, customerRdcocode=null, customerLocked=null, customerBond=null, tblTas2orderConfigsForTas2ocSupplierNumber=[], customerTelNo=null, customerInvoiceCounty=null, customerIsHoldingAccount=null, tblJournalCustomerses=[], tblFrequency=null, customerIsOperator=null, customerAddress1=null, customerAddress2=null, customerInvoiceTown=null}
    DEBUG - Inserting collection: [com.xpagesbeast.data.entities.enh.TblCustomers.tblContactses#37]
    DEBUG - Collection was empty
    DEBUG - committed JDBC Connection
    main.java  closing hibernate session.
    DEBUG - Releasing JDBC connection
    DEBUG - Released JDBC connection
    main.java  session closed.
    DEBUG - Connection pool now considered primed; min-size will be maintained
java sql-server hibernate
1个回答
1
投票

更改实体类TblCustomers中的字段,其类型可序列化为String。

例如:改变 private Serializable customerCode;

private String customerCode;

也可以更改相应的setter和getter方法。

然后尝试运行您的应用程序

© www.soinside.com 2019 - 2024. All rights reserved.