[英]Config Spring for works with hibernate and entities mapped by annotations
im using hibernate and i maps my entities with annotations (so no xml files). 即时通讯使用hibernate,我用注释映射我的实体(所以没有xml文件)。
I finally decided to try spring framework but I encountered some problems to make it work. 我终于决定尝试spring框架,但是我遇到了一些问题让它工作。
All tutorials i found are very dispersive and most of them use xml file to map an entity... Can you help me to correctly write a xml config file for spring+hibernate? 我找到的所有教程都非常分散,大多数使用xml文件来映射实体...你能帮我正确编写spring + hibernate的xml配置文件吗?
Thanks. 谢谢。
The following is a working example from one of my apps. 以下是我的一个应用程序的工作示例。 They should all go in the applicationContext or in a .xml loaded by the appContext.
它们都应该放在applicationContext或appContext加载的.xml中。
The first snippet is the configuration of the datasource, using connection pooling: 第一个片段是使用连接池的数据源配置:
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
p:driverClass="${jdbc.driverClassName}"
p:jdbcUrl="${jdbc.url}"
p:user="${jdbc.username}"
p:password="${jdbc.password}" />
Next up is a Property bean. 接下来是Property bean。 If you are unsure about any of these settings, please refer to the corresponding APIs.
如果您不确定这些设置,请参阅相应的API。
<bean id="hibernateProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.c3p0.minPoolSize">5</prop>
<prop key="hibernate.c3p0.maxPoolSize">20</prop>
<prop key="hibernate.c3p0.idleTestPeriod">300</prop>
<prop key="hibernate.c3p0.timeout">600</prop>
<prop key="hibernate.c3p0.max_statement">50</prop>
<prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
<prop key="hibernate.c3p0.preferredTestQuery">select 1;</prop>
</props>
</property>
</bean>
Now this is the interesting part. 现在这是有趣的部分。 Here you wire it all together and tell the sessionfactory where to look for annotated Classes (packagesToScan).
在这里,您将它们连接在一起并告诉sessionfactory在哪里查找带注释的类(packagesToScan)。
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="your.package.path"
p:hibernateProperties-ref="hibernateProps" />
To make this example work, you should use the following dependencies (given for maven): 要使此示例有效,您应该使用以下依赖项(为maven指定):
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.7.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.0.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.3.0.ga</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency>
When you have setup your project like this, the following @Entity mappings are managed by spring automatically: 当您像这样设置项目时,以下@Entity映射由spring自动管理:
package your.package.path;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name = "table_name")
public class DomainObject implements Serializable {
.
.
.
}
If you have any further questions, please let me know. 如果您还有其他问题,请告诉我。
greetings 问候
If you use JPA ( EntityManager
), the @Entity
classes are discovered automatically. 如果使用JPA(
EntityManager
),则会自动发现@Entity
类。
In spring you obtain the EntityManager
via the LocalEntityManagerFactoryBean
, and then injecting @PersistenceContext
into your classes. 在Spring中,您通过
LocalEntityManagerFactoryBean
获取EntityManager
,然后将@PersistenceContext
注入您的类。 Check here for more info 点击此处了解更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.