Mysql Spring JPA Hibernate写入数据库失败(由于没有正在进行的事务,身份插入延迟)

Mysql Spring JPA Hibernate写入数据库失败(由于没有正在进行的事务,身份插入延迟),mysql,hibernate,spring,jpa-2.0,Mysql,Hibernate,Spring,Jpa 2.0,我使用的是Spring3.0.5.RELEASE和Hibernate3.6.1.Final。从MySql数据库读取是可以的,但是当我尝试写入它时,我在日志中看到: 22 mars 2011 22:37:34,625 DEBUG InjectionMetadata: Processing injected method of bean 'thoughtDao': PersistenceElement for protected javax.persistence.EntityManager com

我使用的是Spring3.0.5.RELEASE和Hibernate3.6.1.Final。从MySql数据库读取是可以的,但是当我尝试写入它时,我在日志中看到:

22 mars 2011 22:37:34,625 DEBUG InjectionMetadata: Processing injected method of bean 'thoughtDao': PersistenceElement for protected javax.persistence.EntityManager com.prosveta.backend.daoimpl.GenericDaoImpl.entityManager
22 mars 2011 22:37:34,625 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'entityManagerFactory'
22 mars 2011 22:37:34,625 DEBUG DefaultListableBeanFactory: Creating shared instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
22 mars 2011 22:37:34,625 DEBUG DefaultListableBeanFactory: Creating instance of bean 'org.springframework.transaction.config.internalTransactionAdvisor'
22 mars 2011 22:37:34,640 DEBUG DefaultListableBeanFactory: Eagerly caching bean 'org.springframework.transaction.config.internalTransactionAdvisor' to allow for resolving potential circular references
22 mars 2011 22:37:34,656 DEBUG DefaultListableBeanFactory: Creating shared instance of singleton bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
22 mars 2011 22:37:34,656 DEBUG DefaultListableBeanFactory: Creating instance of bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
22 mars 2011 22:37:34,656 DEBUG DefaultListableBeanFactory: Eagerly caching bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0' to allow for resolving potential circular references
22 mars 2011 22:37:34,671 DEBUG DefaultListableBeanFactory: Finished creating instance of bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
22 mars 2011 22:37:34,671 DEBUG DefaultListableBeanFactory: Finished creating instance of bean 'org.springframework.transaction.config.internalTransactionAdvisor'
22 mars 2011 22:37:34,718 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
22 mars 2011 22:37:34,734 DEBUG JdkDynamicAopProxy: Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [com.prosveta.backend.daoimpl.thought.ThoughtDaoImpl@d337d3]
22 mars 2011 22:37:34,765 DEBUG DefaultListableBeanFactory: Finished creating instance of bean 'thoughtDao'
22 mars 2011 22:37:34,765 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
22 mars 2011 22:37:34,765 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
22 mars 2011 22:37:34,765 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
22 mars 2011 22:37:34,765 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
22 mars 2011 22:37:34,765 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor'
22 mars 2011 22:37:34,765 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0'
22 mars 2011 22:37:34,765 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'prosvetaDataSource'
22 mars 2011 22:37:34,765 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'entityManagerFactory'
22 mars 2011 22:37:34,765 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0'
22 mars 2011 22:37:34,765 DEBUG DefaultListableBeanFactory: Creating shared instance of singleton bean 'transactionManager'
22 mars 2011 22:37:34,765 DEBUG DefaultListableBeanFactory: Creating instance of bean 'transactionManager'
22 mars 2011 22:37:34,796 DEBUG DefaultListableBeanFactory: Eagerly caching bean 'transactionManager' to allow for resolving potential circular references
22 mars 2011 22:37:34,796 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'entityManagerFactory'
22 mars 2011 22:37:34,796 DEBUG DefaultListableBeanFactory: Invoking afterPropertiesSet() on bean with name 'transactionManager'
22 mars 2011 22:37:34,796 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
22 mars 2011 22:37:34,812 DEBUG DefaultListableBeanFactory: Finished creating instance of bean 'transactionManager'
22 mars 2011 22:37:34,812 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.aop.config.internalAutoProxyCreator'
22 mars 2011 22:37:34,812 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
22 mars 2011 22:37:34,812 DEBUG DefaultListableBeanFactory: Creating shared instance of singleton bean 'org.springframework.transaction.interceptor.TransactionInterceptor#0'
22 mars 2011 22:37:34,812 DEBUG DefaultListableBeanFactory: Creating instance of bean 'org.springframework.transaction.interceptor.TransactionInterceptor#0'
22 mars 2011 22:37:34,812 DEBUG DefaultListableBeanFactory: Eagerly caching bean 'org.springframework.transaction.interceptor.TransactionInterceptor#0' to allow for resolving potential circular references
22 mars 2011 22:37:34,812 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
22 mars 2011 22:37:34,812 DEBUG DefaultListableBeanFactory: Invoking afterPropertiesSet() on bean with name 'org.springframework.transaction.interceptor.TransactionInterceptor#0'
22 mars 2011 22:37:34,812 DEBUG DefaultListableBeanFactory: Finished creating instance of bean 'org.springframework.transaction.interceptor.TransactionInterceptor#0'
22 mars 2011 22:37:34,812 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
22 mars 2011 22:37:34,812 DEBUG DefaultListableBeanFactory: Creating shared instance of singleton bean 'thoughtDaoBean'
22 mars 2011 22:37:34,828 DEBUG DefaultListableBeanFactory: Creating instance of bean 'thoughtDaoBean'
22 mars 2011 22:37:34,828 DEBUG DefaultListableBeanFactory: Eagerly caching bean 'thoughtDaoBean' to allow for resolving potential circular references
22 mars 2011 22:37:34,828 DEBUG InjectionMetadata: Processing injected method of bean 'thoughtDaoBean': PersistenceElement for protected javax.persistence.EntityManager com.prosveta.backend.daoimpl.GenericDaoImpl.entityManager
22 mars 2011 22:37:34,828 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'entityManagerFactory'
22 mars 2011 22:37:34,828 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
22 mars 2011 22:37:34,828 DEBUG JdkDynamicAopProxy: Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [com.prosveta.backend.daoimpl.thought.ThoughtDaoImpl@1f2af1c]
22 mars 2011 22:37:34,828 DEBUG DefaultListableBeanFactory: Finished creating instance of bean 'thoughtDaoBean'
22 mars 2011 22:37:34,828 DEBUG ClassPathXmlApplicationContext: Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@a36b53]
22 mars 2011 22:37:34,828 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'lifecycleProcessor'
22 mars 2011 22:37:34,828 DEBUG DefaultListableBeanFactory: Returning cached instance of singleton bean 'thoughtDaoBean'
22 mars 2011 22:37:34,859 DEBUG SharedEntityManagerCreator$SharedEntityManagerInvocationHandler: Creating new EntityManager for shared EntityManager invocation
22 mars 2011 22:37:34,968 DEBUG SessionImpl    : opened session at timestamp: 5328199085629440
22 mars 2011 22:37:35,031 DEBUG AbstractSaveEventListener: delaying identity-insert due to no transaction in progress
22 mars 2011 22:37:35,031 DEBUG EntityManagerFactoryUtils: Closing JPA EntityManager
使用AbstractSaveEventListener:end前一行没有正在进行的事务,因此延迟标识插入

我已经搜索了很长时间,将我的Spring配置文件与网络上的其他文件进行了比较,但没有结果

以下是配置文件:

mysql连接器

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.15</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>
spring master dao test.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="prosvetaPersistenceUnit"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.prosveta.backend.model.thought.Thought</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <!-- 
                value='create' to build a new database on each run;
                value='update' to modify an existing database;
                value='create-drop' means the same as 'create' but also drops tables when Hibernate closes;
                value='validate' makes no changes to the database
            -->
            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.cache.use_query_cache" value="true"/>
            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider"/>
            <!-- 
            <property name="hibernate.search.default.directory_provider" value="org.hibernate.search.store.FSDirectoryProvider"/>
            <property name="hibernate.search.default.indexBase" value="./lucene/indexes"/>
            <property name="hibernate.search.default.batch.merge_factor" value="10"/>
            <property name="hibernate.search.default.batch.max_buffered_docs" value="10"/>
             -->
        </properties>
    </persistence-unit>

</persistence>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- JPA Entity Manager Factory -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:dataSource-ref="prosvetaDataSource">
        <property name="persistenceUnitName" value="prosvetaPersistenceUnit" />
    </bean>

    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <!-- Database LOB Handling -->
    <!-- bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" / -->

    <!-- Read in DAOs from the JPA package -->
    <context:component-scan base-package="com.prosveta.backend.daoimpl" />

    <!-- Transaction Config -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
        p:entityManagerFactory-ref="entityManagerFactory" />

    <tx:annotation-driven mode="aspectj"
        transaction-manager="transactionManager" />

</beans>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

    <!-- Basic DataSource -->
    <bean id="prosvetaDataSource"
          class="org.apache.commons.dbcp.BasicDataSource" 
          p:driverClassName="${jdbc.driverClassName}" 
          p:url="${jdbc.url}" 
          p:username="${jdbc.username}"
          p:password="${jdbc.password}"/>

</beans>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/util
                           http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>

    <context:component-scan base-package="com.prosveta.backend">
        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>

    <!-- This will ensure that hibernate or jpa exceptions are automatically translated into
         Spring's generic DataAccessException hierarchy -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

    <import resource="spring-datasource.xml"/>
    <import resource="spring-jpa.xml"/>

    <!-- required only for main method in ThoughtDaoImpl used to investigate
    the delayed db creation problem -->
    <bean id="thoughtDaoBean" class="com.prosveta.backend.daoimpl.thought.ThoughtDaoImpl" />

</beans>

现在是java代码

模型类

# Properties file with JDBC-related settings applied by PropertyPlaceholderConfigurer.

jdbc.url=jdbc:mysql://localhost/p_joo15_dev?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=pw
package com.prosveta.backend.model.thought;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.QueryHint;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.prosveta.backend.model.DomainObject;
import com.prosveta.backend.model.language.Language;

@SuppressWarnings("serial")
@Entity
@Table(name="jos_thoughts_p")
@NamedQueries({
        @NamedQuery(name = "Thought.getThought", 
                    query = "select thought from Thought thought where thought.date = :date and thought.language = :language",
                    hints={@QueryHint(name="org.hibernate.cacheable",value="true")}),
        @NamedQuery(name = "Thought.getAllThoughtIdsBeforeDate", 
                    query = "select thought.id from Thought thought where thought.date < :date and thought.language = :language",
                    hints={@QueryHint(name="org.hibernate.cacheable",value="true")}),
        // it seems not a good idea to cache the query below since the 2 dates doublet has few chances to be used often in a short period of time !
        @NamedQuery(name = "Thought.getAllThoughtIdsBeforeDateExceptExclusionDate", 
                    query = "select thought.id from Thought thought where thought.date < :date and thought.language = :language and thought.date != :exclusionDate"),
})
public class Thought implements DomainObject {
    private Integer id;
    private Date date;
    private String language;
    private Integer treeOfLife;
    private Integer twoNatures;
    private String text;
    private String keyword;
    private String sentence;

    public Thought() {
    }

    public Thought(Integer id, Date date, String language, Integer treeOfLife,
            Integer twoNatures, String text, String keyword, String sentence) {
        super();
        this.id = id;
        this.date = date;
        this.language = language;
        this.treeOfLife = treeOfLife;
        this.twoNatures = twoNatures;
        this.text = text;
        this.keyword = keyword;
        this.sentence = sentence;
    }

    @Id
    @GeneratedValue
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Temporal(TemporalType.DATE)
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    /**
     * Returns the Language enum denoting the thought language 
     * @return
     */
    @Transient
    public Language getLanguageEnum() {
        return Language.valueOf(language);
    }

    @Column(name = "tree_of_life", columnDefinition="SMALLINT")
    public Integer getTreeOfLife() {
        return treeOfLife;
    }

    public void setTreeOfLife(Integer treeOfLife) {
        this.treeOfLife = treeOfLife;
    }

    @Column(name = "2_natures", columnDefinition="SMALLINT")
    public Integer getTwoNatures() {
        return twoNatures;
    }

    public void setTwoNatures(Integer twoNatures) {
        this.twoNatures = twoNatures;
    }

    @Column(columnDefinition="LONGTEXT")
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public String getSentence() {
        return sentence;
    }

    public void setSentence(String sentence) {
        this.sentence = sentence;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Thought [id=" + id + ", date=" + date + ", language="
                + language + ", treeOfLife=" + treeOfLife + ", twoNatures="
                + twoNatures + ", text=" + text + ", keyword=" + keyword
                + ", sentence=" + sentence + "]";
    }
}
package com.prosveta.backend.daoimpl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.dao.DataAccessException;

import com.prosveta.backend.dao.GenericDao;
import com.prosveta.backend.model.DomainObject;

/*
 * This class contains a generic JPA implementation of
 * the CRUD operations provided by dao's.
 */
@SuppressWarnings("unchecked")
public class GenericDaoImpl<T extends DomainObject> implements GenericDao<T> {

    private Class<T> type;

    @PersistenceContext
    protected EntityManager entityManager;

    public GenericDaoImpl(Class<T> type) {
        super();
        this.type = type;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public EntityManager getEntityManager() {
        return this.entityManager;
    }

    public T get(Long id) {
        return (T) this.entityManager.find(type, id);
    }

    public List<T> getAll() {
        return this.entityManager.createQuery(
                "select obj from " + type.getName() + " obj").getResultList();
    }

    public void save(T object) throws DataAccessException {
        this.entityManager.persist(object);
    }

    public void delete(T object) throws DataAccessException {
        this.entityManager.remove(object);
    }
}
package com.prosveta.backend.model.think;
导入java.util.Date;
导入javax.persistence.Column;
导入javax.persistence.Entity;
导入javax.persistence.GeneratedValue;
导入javax.persistence.Id;
导入javax.persistence.namedquerys;
导入javax.persistence.NamedQuery;
导入javax.persistence.QueryHint;
导入javax.persistence.Table;
导入javax.persistence.Temporal;
导入javax.persistence.TemporalType;
导入javax.persistence.Transient;
导入com.prosveta.backend.model.DomainObject;
导入com.prosveta.backend.model.language.language;
@抑制警告(“串行”)
@实体
@表(name=“jos\u-p”)
@命名查询({
@NamedQuery(name=“think.getthough”,
query=“从think think中选择think,其中think.date=:date和think.language=:language”,
提示={@QueryHint(name=“org.hibernate.cacheable”,value=“true”)},
@NamedQuery(name=“think.getAllThoughtIDBeforedate”,
query=“从think think中选择think.id,其中think.date<:date和think.language=:language”,
提示={@QueryHint(name=“org.hibernate.cacheable”,value=“true”)},
//缓存下面的查询似乎不是一个好主意,因为2个日期的doublet很少有机会在短时间内经常使用!
@NamedQuery(name=“think.getAllThoughtIDsBeforedateExceptionExclusionDate”,
query=“从think think中选择think.id,其中think.date<:date和think.language=:language和think.date!=:exclusionDate”),
})
公共类思想实现了DomainObject{
私有整数id;
私人日期;
私有字符串语言;
私有整数树flife;
私有整数性质;
私有字符串文本;
私有字符串关键字;
私刑;
公众思想{
}
公共思想(整数id、日期、字符串语言、整数树flife、,
整数(性质、字符串文本、字符串关键字、字符串句子){
超级();
this.id=id;
this.date=日期;
这种语言=语言;
this.treeOfLife=treeOfLife;
this.twoNatures=twoNatures;
this.text=文本;
this.keyword=关键字;
这个句子=句子;
}
@身份证
@生成值
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
@时态(TemporalType.DATE)
公共日期getDate(){
返回日期;
}
公共作废设置日期(日期){
this.date=日期;
}
公共字符串getLanguage(){
返回语言;
}
公共语言(字符串语言){
这种语言=语言;
}
/**
*返回表示思维语言的语言枚举
*@返回
*/
@短暂的
公共语言getLanguageEnum(){
返回语言。valueOf(语言);
}
@列(name=“生命之树”,columnDefinition=“SMALLINT”)
公共整数getTreeOfLife(){
返回树飞行;
}
public void setTreeOfLife(整数treeOfLife){
this.treeOfLife=treeOfLife;
}
@列(name=“2_natures”,columnDefinition=“SMALLINT”)
公共整数getTwoNatures(){
回归两性;
}
public void setTwoNatures(整数twoNatures){
this.twoNatures=twoNatures;
}
@列(columnDefinition=“LONGTEXT”)
公共字符串getText(){
返回文本;
}
公共void setText(字符串文本){
this.text=文本;
}
公共字符串getKeyword(){
返回关键字;
}
public void setKeyword(字符串关键字){
this.keyword=关键字;
}
公共字符串get句子(){
返回判决;
}
公共无效设置项(字符串句){
这个句子=句子;
}
/*(非Javadoc)
*@see java.lang.Object#toString()
*/
@凌驾
公共字符串toString(){
return“think[id=“+id+”,date=“+date+”,language=”
+language+”,treeOfLife=“+treeOfLife+”,twoNatures=”
+twoNatures+”,text=“+text+”,keyword=“+keyword
+“,句子=”+句子+“]”;
}
}
通用dao基类

# Properties file with JDBC-related settings applied by PropertyPlaceholderConfigurer.

jdbc.url=jdbc:mysql://localhost/p_joo15_dev?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=pw
package com.prosveta.backend.model.thought;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.QueryHint;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.prosveta.backend.model.DomainObject;
import com.prosveta.backend.model.language.Language;

@SuppressWarnings("serial")
@Entity
@Table(name="jos_thoughts_p")
@NamedQueries({
        @NamedQuery(name = "Thought.getThought", 
                    query = "select thought from Thought thought where thought.date = :date and thought.language = :language",
                    hints={@QueryHint(name="org.hibernate.cacheable",value="true")}),
        @NamedQuery(name = "Thought.getAllThoughtIdsBeforeDate", 
                    query = "select thought.id from Thought thought where thought.date < :date and thought.language = :language",
                    hints={@QueryHint(name="org.hibernate.cacheable",value="true")}),
        // it seems not a good idea to cache the query below since the 2 dates doublet has few chances to be used often in a short period of time !
        @NamedQuery(name = "Thought.getAllThoughtIdsBeforeDateExceptExclusionDate", 
                    query = "select thought.id from Thought thought where thought.date < :date and thought.language = :language and thought.date != :exclusionDate"),
})
public class Thought implements DomainObject {
    private Integer id;
    private Date date;
    private String language;
    private Integer treeOfLife;
    private Integer twoNatures;
    private String text;
    private String keyword;
    private String sentence;

    public Thought() {
    }

    public Thought(Integer id, Date date, String language, Integer treeOfLife,
            Integer twoNatures, String text, String keyword, String sentence) {
        super();
        this.id = id;
        this.date = date;
        this.language = language;
        this.treeOfLife = treeOfLife;
        this.twoNatures = twoNatures;
        this.text = text;
        this.keyword = keyword;
        this.sentence = sentence;
    }

    @Id
    @GeneratedValue
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Temporal(TemporalType.DATE)
    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    /**
     * Returns the Language enum denoting the thought language 
     * @return
     */
    @Transient
    public Language getLanguageEnum() {
        return Language.valueOf(language);
    }

    @Column(name = "tree_of_life", columnDefinition="SMALLINT")
    public Integer getTreeOfLife() {
        return treeOfLife;
    }

    public void setTreeOfLife(Integer treeOfLife) {
        this.treeOfLife = treeOfLife;
    }

    @Column(name = "2_natures", columnDefinition="SMALLINT")
    public Integer getTwoNatures() {
        return twoNatures;
    }

    public void setTwoNatures(Integer twoNatures) {
        this.twoNatures = twoNatures;
    }

    @Column(columnDefinition="LONGTEXT")
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public String getSentence() {
        return sentence;
    }

    public void setSentence(String sentence) {
        this.sentence = sentence;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Thought [id=" + id + ", date=" + date + ", language="
                + language + ", treeOfLife=" + treeOfLife + ", twoNatures="
                + twoNatures + ", text=" + text + ", keyword=" + keyword
                + ", sentence=" + sentence + "]";
    }
}
package com.prosveta.backend.daoimpl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.dao.DataAccessException;

import com.prosveta.backend.dao.GenericDao;
import com.prosveta.backend.model.DomainObject;

/*
 * This class contains a generic JPA implementation of
 * the CRUD operations provided by dao's.
 */
@SuppressWarnings("unchecked")
public class GenericDaoImpl<T extends DomainObject> implements GenericDao<T> {

    private Class<T> type;

    @PersistenceContext
    protected EntityManager entityManager;

    public GenericDaoImpl(Class<T> type) {
        super();
        this.type = type;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public EntityManager getEntityManager() {
        return this.entityManager;
    }

    public T get(Long id) {
        return (T) this.entityManager.find(type, id);
    }

    public List<T> getAll() {
        return this.entityManager.createQuery(
                "select obj from " + type.getName() + " obj").getResultList();
    }

    public void save(T object) throws DataAccessException {
        this.entityManager.persist(object);
    }

    public void delete(T object) throws DataAccessException {
        this.entityManager.remove(object);
    }
}
package com.prosveta.backend.daoimpl;
导入java.util.List;
导入javax.persistence.EntityManager;
导入javax.persistence.PersistenceContext;
导入org.springframework.dao.DataAccessException;
导入com.prosveta.backend.dao.GenericDao;
导入com.prosveta.backend.model.DomainObject;
/*
*此类包含的通用JPA实现
*dao提供的CRUD操作。
*/
@抑制警告(“未选中”)
公共类GenericDaoImpl实现GenericDao{
私人阶级类型;
@持久上下文
受保护的实体管理器实体管理器;
公共GenericDaoImpl(类类型){
超级();
this.type=type;
}
公共无效设置EntityManager(EntityManager EntityManager){
this.entityManager=entityManager;
}
公共实体管理器getEntityManager(){
返回此.entityManager;
}
普布利