Reputation: 816
I used JBOSS Hibernate tools to reverse engineer the MS SQL Server to get my data model objects.
I then wrote a small test program in Java to open a session, use the configuration, instantiate a Customer object, persist it into the database.
The sample runs writing one record to the database and closing the session.
What is weird, I have never seen this before is the 'oriental' characters being saved into the database columns.
Database:
SQL Server (Express) 10.50.1600 on Windows 2012 R2 Standard
Columns customerCode and customerName are nvarchar, null
Java App: Eclipse Neon, Maven, Hibernate 4.3, Java 1.8
NOTE: I am a newbie to Hibernate.
What else can I try or refactor?
Things I tried Do a SQL Insert with IBM data studio (success)
No Success
public class SqlServerDialectWithNvarchar extends 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)");
}
}
Main Program:
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 Query using IBM Data Studio after main program runs
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>
Customer Class /** * TblCustomers generated by hbm2java */
@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;
}
Console Output (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
Upvotes: 0
Views: 273
Reputation: 26
Change the fields in your entity class TblCustomers, whose type are serializable to String.
For eg: change
private Serializable customerCode;
to
private String customerCode;
Change the corresponding setter and getter methods as well.
Then try running your application.
Upvotes: 1