这是主要的班级代码
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
EmployeeService employeeService = context.getBean(EmployeeService.class);
Employee emp = new Employee();
if(emp.getHdEmpId()==0) {
emp.setTxtaEmployeeAddress("abcde");
emp.setTxtEmployeeEmail("abcd@localmail.com");
emp.setChkHobby("cricket");
emp.setRdGender("Male");
emp.setTxtEmployeeName("sumit");
emp.setTxtEmployeePhone("451254");
emp.setTxtEmployeeSalary("100");
emp.setSelEmployeeCountry("India");
emp.setHdActive(true);
emp.setTxtEmployeeTinyIntValue((short)1);
emp.setTxtEmployeeIntegerValue(11111);
emp.setTxtEmployeeTotalSalary(45454.56556);
BigDecimal bd=new BigDecimal("12.36");
emp.setTxtEmployeeMobile(bd);
emp.setTxtEmployeeEmp4(new Long("7845784575"));
employeeService.addEmployee(emp);
}
模型类
package com.eptl.spring4hibernate4demo.employee.model;
import java.math.BigDecimal;
import java.sql.Types;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Type;
import org.hibernate.type.BigDecimalType;
@Entity
@Table(name="Emp")
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int hdEmpId;
@Column(name="name")
private String txtEmployeeName;
@Column(name="phone")
private String txtEmployeePhone;
@Column(name="salary")
private String txtEmployeeSalary;
@Column(name="email")
private String txtEmployeeEmail;
@Column(name="address")
private String txtaEmployeeAddress;
@Column(name="gender")
private String rdGender;
@Column(name="country")
private String selEmployeeCountry;
@Column(name="hobbies")
private String chkHobby;
@Column(name="isActive")
private boolean hdActive;
@Column(name="Mobile",precision=18,scale=5,columnDefinition="decimal(18,5)")
@Type(type = "big_decimal")
private BigDecimal txtEmployeeMobile;
@Column(name="totalSalary")
private Double txtEmployeeTotalSalary;
@Column(name="emp1")
private Integer txtEmployeeIntegerValue;
@Column(name="emp2")
private Short txtEmployeeTinyIntValue;
@Column(name="emp4")
private Long txtEmployeeEmp4;
@Column(precision=18,scale=5)
public BigDecimal getTxtEmployeeMobile() {
return txtEmployeeMobile;
}
public void setTxtEmployeeMobile(BigDecimal txtEmployeeMobile) {
this.txtEmployeeMobile = txtEmployeeMobile;
}
public Long getTxtEmployeeEmp4() {
return txtEmployeeEmp4;
}
public void setTxtEmployeeEmp4(Long txtEmployeeEmp4) {
this.txtEmployeeEmp4 = txtEmployeeEmp4;
}
public int getHdEmpId() {
return hdEmpId;
}
public void setHdEmpId(int hdEmpId) {
this.hdEmpId = hdEmpId;
}
public String getTxtEmployeeName() {
return txtEmployeeName;
}
public void setTxtEmployeeName(String txtEmployeeName) {
this.txtEmployeeName = txtEmployeeName;
}
public String getTxtEmployeePhone() {
return txtEmployeePhone;
}
public void setTxtEmployeePhone(String txtEmployeePhone) {
this.txtEmployeePhone = txtEmployeePhone;
}
public String getTxtEmployeeSalary() {
return txtEmployeeSalary;
}
public void setTxtEmployeeSalary(String txtEmployeeSalary) {
this.txtEmployeeSalary = txtEmployeeSalary;
}
public String getTxtEmployeeEmail() {
return txtEmployeeEmail;
}
public void setTxtEmployeeEmail(String txtEmployeeEmail) {
this.txtEmployeeEmail = txtEmployeeEmail;
}
public String getTxtaEmployeeAddress() {
return txtaEmployeeAddress;
}
public void setTxtaEmployeeAddress(String txtaEmployeeAddress) {
this.txtaEmployeeAddress = txtaEmployeeAddress;
}
public String getRdGender() {
return rdGender;
}
public void setRdGender(String rdGender) {
this.rdGender = rdGender;
}
public String getSelEmployeeCountry() {
return selEmployeeCountry;
}
public void setSelEmployeeCountry(String selEmployeeCountry) {
this.selEmployeeCountry = selEmployeeCountry;
}
public String getChkHobby() {
return chkHobby;
}
public void setChkHobby(String chkHobby) {
this.chkHobby = chkHobby;
}
public boolean isHdActive() {
return hdActive;
}
public void setHdActive(boolean hdActive) {
this.hdActive = hdActive;
}
public Double getTxtEmployeeTotalSalary() {
return txtEmployeeTotalSalary;
}
public void setTxtEmployeeTotalSalary(Double txtEmployeeTotalSalary) {
this.txtEmployeeTotalSalary = txtEmployeeTotalSalary;
}
public Integer getTxtEmployeeIntegerValue() {
return txtEmployeeIntegerValue;
}
public void setTxtEmployeeIntegerValue(Integer txtEmployeeIntegerValue) {
this.txtEmployeeIntegerValue = txtEmployeeIntegerValue;
}
public Short getTxtEmployeeTinyIntValue() {
return txtEmployeeTinyIntValue;
}
public void setTxtEmployeeTinyIntValue(Short txtEmployeeTinyIntValue) {
this.txtEmployeeTinyIntValue = txtEmployeeTinyIntValue;
}
}
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.eptl.spring4hibernate4demo.employee.*"/>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://192.168.100.40:1433;databaseName=EmpEnc;integratedSecurity=true;columnEncryptionSetting=Enabled"/>
<property name="username" value="abcd" />
<property name="password" value="abc@123" />
<property name="maxActive" value="100"/>
<property name="maxWait" value="10000"/>
<property name="maxIdle" value="10"/>
<property name="testWhileIdle" value="true"/>
<property name="validationQuery" value="Select 1"/>
<property name="timeBetweenEvictionRunsMillis" value="1800000"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>com.eptl.spring4hibernate4demo.**.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2012Dialect</prop>
<prop key="current_session_context_class">thread</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.max_fetch_depth">5</prop>
<prop key="hibernate.default_batch_fetch_size">16</prop>
<prop key="hibernate.jdbc.batch_size">25</prop>
<prop key="hibernate.jdbc.fetch_size">8</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.release_mode">after_transaction</prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
错误:
> log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into Emp (hobbies, isActive, gender, country, email, emp4, emp1, Mobile, name, phone, salary, emp2, totalSalary, address) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:84)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2730)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3300)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:474)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:179)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:163)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:198)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:317)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:272)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:109)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:678)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:670)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:665)
at com.eptl.spring4hibernate4demo.employee.dao.AbcAbstractClass.addEntity(AbcAbstractClass.java:61)
at com.eptl.spring4hibernate4demo.employee.daoImpl.EmployeeImpl.addEmployee(EmployeeImpl.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy23.addEmployee(Unknown Source)
at com.eptl.spring4hibernate4demo.employee.service.EmployeeService.addEmployee(EmployeeService.java:22)
at com.eptl.spring4hibernate4demo.employee.controller.EmpController.main(EmpController.java:150)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Operand type clash: decimal(18,2) encrypted with (encryption_type = 'DETERMINISTIC', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'empencCEK', column_encryption_key_database_name = 'EmpEnc') is incompatible with decimal(18,5) encrypted with (encryption_type = 'DETERMINISTIC', encryption_algorithm_name = 'AEAD_AES_256_CBC_HMAC_SHA_256', column_encryption_key_name = 'empencCEK', column_encryption_key_database_name = 'EmpEnc')
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1655)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:440)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:385)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:166)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQueryInternal(SQLServerPreparedStatement.java:310)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.getParameterEncryptionMetadata(SQLServerPreparedStatement.java:666)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:422)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:385)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:166)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:328)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 38 more
要插入值,请使用会话
// DaoImpl method
public void addEmployee(Employee employee) {
employeeDao.addEmployee(employee);
}
// method moves on addEmployee
@Override
public void addEmployee(Employee employee) {
super.addEntity(employee);
}
// and finally common class to save entity
public void addEntity(EntityType entity) {
getCurrentSession().save(entity);
}
当我在没有加密列的情况下将十进制值添加到数据库中时,它将存储在数据库中,但是当我加密列时会显示错误。我完全设置了属性。我试图在数据库方面增加价值 它完全插入到数据库中,但是当我尝试使用休眠方式插入值时,它显示了一个错误。 如何使用休眠将大十进制值插入始终加密的数据库中?