2017-12-28 21 views
0

私は、JBossのHibernateツールを使用して、データモデルオブジェクトを取得するためにMS SQL Serverをリバースエンジニアリングしました。Hibernate SQL Serverにラテン記号の代わりにオリエンタルを書く

私は、Javaでセッションを開き、設定を使用し、Customerオブジェクトをインスタンス化し、データベースに永続化するための小さなテストプログラムを作成しました。

サンプルは、1つのレコードをデータベースに書き込んでセッションを閉じます。

私はこれまで、「オリエンタル」文字がデータベース列に保存されることはありませんでした。

データベース:Eclipseのネオン、Mavenの、4.3を休止、Javaの1.8:customerCodeとCUSTOMERNAMEはnvarchar型のWindows 2012 R2の標準
列、ヌル
のJavaアプリケーションに10.50.1600

のSQL Server(エクスプレス)

注:私は休止状態の初心者です。

他に何か試したり、リファクタリングすることができますか?

物事は私がIBMデータ・スタジオ(成功)

ありません成功

  1. UTF-8接続URL
  2. では、休止状態にUTF-8を追加してSQL挿入を行い を試してみましたxml
  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)"); 
} 

}

メインプログラム拡張:メインプログラムの実行後にIBM Data Studioのを使用して

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

} 

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> 

顧客クラス /** * */

@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 =真)hbm2javaによって生成TblCustomers

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 
+0

データベースの文字エンコーディングはどうですか? –

+0

Latin1_General_CI_AS from information_schema.columns – xpagesbeast

+0

SELECT collat​​ion_name FROM sys.databases WHERE name = 'TAS'; Latin1_General_CI_AS – xpagesbeast

答えて

1

エンティティクラスTblCustomersのフィールドを変更します。タイプは、String型にシリアライズ可能です。例えばのために

:変更
private Serializable customerCode;

対応するsetterメソッドとgetterメソッドの変更

private String customerCode; 

に同様。

次に、アプリケーションを実行してみてください。

+0

を返します。Serializableの代わりにStringを使用すると、Alex Jeyasignhさん、問題を解決しました。 – xpagesbeast

+0

なぜ、hbm2javaはPOJOを生成するときにStringの代わりにSerializableを主張するのですか?生成されたすべてのPOJOをStringに変更しないように、Serializable出力に正しいエンコーディングをさせるために何かできることはありますか? – xpagesbeast

関連する問題