我是Spring和Hibernate的新手。 我配置了数据源。 当我制作实体类时,出现错误(在Idea中),该架构无法解析。在Idea中,模式,表,列的所有定义都用红色下划线标出。 该对象还可以,但是当我尝试使用HibernateTemplate.save时,却收到错误消息:
[10/7/18 7:58:46:284 MSK] 00000169 SystemOut O SMSDaoImpl.saveSMS:{“ deliveryTime”:null,“ id”:null,“ idExt”:123,“ message”:“ Test “,” phoneAb“:” 71234567890“,” sendTime“:1538888326275,” staffId“:99001446,” staffIdAb“:99000039,” status“:null} [10/7/18 7:58:46:344 MSK] 00000169 DispatcherPor E org.springframework.web.portlet.FrameworkPortlet processRequest无法完成请求 org.springframework.dao.DataIntegrityViolationException:无法插入:[com.technoserv.SMS.entity.SMS]; SQL [插入ORG.SMS(ID,DELIVERY_TIME,ID_EXT,MESSAGE,PHONE_AB,SEND_TIME,STAFF_ID,STAFF_ID_AB,STATUS)值(默认值,?,?,?,?,?,?,?,?));嵌套的异常是org.hibernate.exception.DataException:无法插入:[com.technoserv.SMS.entity.SMS]
怎么了?预先感谢。
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- !DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="WASDataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"
value="DB2_ds"/>
<property name="lookupOnStartup"
value="false"/>
<property name="cache"
value="false"/> <!-- ?? -->
<property name="proxyInterface"
value="javax.sql.DataSource"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="WASDataSource" />
<property name="configLocation" value="classpath:/hibernate.cfg.xml" />
</bean>
<bean id="smsDAO" class="com.technoserv.SMS.dao.SMSDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="smsService" class="com.technoserv.SMS.service.SMSServiceImpl">
<property name="dao" ref="smsDAO"/>
</bean>
</beans>
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">DB2_ds</property>
<property name="connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.DB2Dialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">false</property>
<mapping class="com.technoserv.SMS.entity.SMS"></mapping>
</session-factory>
</hibernate-configuration>
SMS.java
package com.technoserv.SMS.entity;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.util.Calendar;
import flexjson.JSONSerializer;
import org.springframework.transaction.annotation.Transactional;
@Entity
@Table(schema = "ORG", name="SMS")
public class SMS {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", nullable = false)
private Integer id;
@Column(name = "ID_EXT")
private Integer idExt;
@Column(name = "STAFF_ID")
private Integer staffId;
@Column(name = "STAFF_ID_AB")
private Integer staffIdAb;
@Column(name = "PHONE_AB")
private String phoneAb;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "dd.MM.yyyy hh:mm:ss a")
@Column(name = "SEND_TIME",columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
private Calendar sendTime;
@Column(name = "MESSAGE")
private String message;
@Column(name = "STATUS")
private String status;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "dd.MM.yyyy hh:mm:ss a")
@Column(name = "DELIVERY_TIME")
private Calendar deliveryTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getIdExt() {
return idExt;
}
public void setIdExt(Integer idExt) {
this.idExt = idExt;
}
public Integer getStaffId() {
return staffId;
}
public void setStaffId(Integer staffId) {
this.staffId = staffId;
}
public Integer getStaffIdAb() {
return staffIdAb;
}
public void setStaffIdAb(Integer staffIdAb) {
this.staffIdAb = staffIdAb;
}
public String getPhoneAb() {
return phoneAb;
}
public void setPhoneAb(String phoneAb) {
this.phoneAb = phoneAb;
}
public Calendar getSendTime() {
return sendTime;
}
public void setSendTime(Calendar sendTime) {
this.sendTime = sendTime;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Calendar getDeliveryTime() {
return deliveryTime;
}
public void setDeliveryTime(Calendar deliveryTime) {
this.deliveryTime = deliveryTime;
}
public String toJson() {
return new JSONSerializer()
.exclude("*.class").serialize(this);
}
@PersistenceContext
transient EntityManager entityManager;
public static final EntityManager entityManager() {
EntityManager em = new SMS().entityManager;
if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
return em;
}
@Transactional
public void persist() {
if (this.entityManager == null) this.entityManager = entityManager();
this.entityManager.persist(this);
}
}
SMSDao.java
package com.technoserv.SMS.dao;
import com.technoserv.SMS.entity.SMS;
public interface SMSDao {
public SMS read(int id);
public void saveSMS(SMS sms);
}
SMSDaoImpl.java
package com.technoserv.SMS.dao;
import com.technoserv.SMS.entity.SMS;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class SMSDaoImpl extends HibernateDaoSupport implements SMSDao{
@Override
public SMS read(int id) {
return getHibernateTemplate().get(SMS.class, id);
}
@Override
public void saveSMS(SMS sms) {
System.out.println("SMSDaoImpl.saveSMS:"+sms.toJson());
HibernateTemplate ht = getHibernateTemplate();
ht.save(sms);
ht.flush();
}
}
SMSService.java
package com.technoserv.SMS.service;
import com.technoserv.SMS.entity.SMS;
public interface SMSService {
public SMS getSMSById(int id);
public void saveSMS (SMS sms);
}
SMSServiceImpl.java
package com.technoserv.SMS.service;
import com.technoserv.SMS.dao.SMSDao;
import com.technoserv.SMS.dao.SMSDaoImpl;
import com.technoserv.SMS.entity.SMS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Transactional
public class SMSServiceImpl implements SMSService{
@Autowired
private SMSDao dao;
@PersistenceContext
private EntityManager em;
@Override
public SMS getSMSById(int id){ return dao.read(id);}
@Override
public void saveSMS(SMS sms) {
System.out.println("saveSMS:"+sms.toJson());
System.out.println("dao:"+dao.toString());
dao.saveSMS(sms);
}
public void setDao(SMSDaoImpl dao) {
this.dao = dao;
}
}