hibernate中的annotations是啥意思
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate中的annotations是啥意思相关的知识,希望对你有一定的参考价值。
hibernate中的annotations是什么意思
刚学hibernate,下了一个包hibernate-annotations-3.3.0.GA.zip,还有hibernate-entitymanager-3.3.1.GA.zip,这两个包是“标准”的Hibernate吗? 和“标准”的Hibernate有什么区别?
不知道你想说的“标准”的hibernate代表什么,先听听我说的。
hibernate最开始是想解决java类和关系型数据库的映射问题的,主要是实现数据库操作问题的自动化,结果效果很好,越来越火爆。后来,java的标准委员会从中大受启发,要把这个思想放到java ee的标准里面,就把hibernate的创始人给请去设计jpa标准。java提供的只是一个标准而已,有许多软件厂商去实现,当然肯定是hibernate实现的最好了。
annotation是java后来增加的一种技术,通过注解就可以让代码实现很多功能。例如在类名上加上@table(name="t_teacher")的注解,相应的框架就会自动在数据库里面给你建好对应的表(当然还要配置文件和库支持)。hibernate-annotations-3.3.0.GA.zip是hibernate提供的annotation,一般不用,用java ee自带的就可以了,命名空间是javax.persistence.annotation(不知道有没有打错哦)。
至于hibernate-entitymanager,是hibernate提供ejb3实体管理的jpa实现,使用EJB3持久化的hibernate支持功能。
一般情况下使用hibernate是不需要这两个包的。 参考技术A annotation就是注解的意思,很多配置的信息不用写配置文件里,直接以编码的方式写在代码里,例:
原来是配置文件里需要写实体类与数据库表之间的映射,以面以注解的方式
@Entity
@Table(name="t_user")
public class User
private int id;
private String username;
private String password;
private String email;
private Date birthday;
private Gender gender;
private Org org;
private List <Role> roles = new ArrayList<Role>();
@ManyToMany
@JoinTable(name="t_user_role" ,joinColumns=@JoinColumn(name="u_id"),inverseJoinColumns=@JoinColumn(name="r_id"))
public List<Role> getRoles()
return roles;
public void setRoles(List<Role> roles)
this.roles = roles;
public void setId(int id)
this.id = id;
@Id
@GeneratedValue
public int getId()
return id;
public void setUsername(String username)
this.username = username;
public String getUsername()
return username;
public void setPassword(String password)
this.password = password;
public String getPassword()
return password;
public void setEmail(String email)
this.email = email;
public String getEmail()
return email;
public void setBirthday(Date birthday)
this.birthday = birthday;
@JSON(format="yyyy-MM-dd")
public Date getBirthday()
return birthday;
public void setGender(Gender gender)
this.gender = gender;
@Enumerated(EnumType.STRING)
public Gender getGender()
return gender;
public void setOrg(Org org)
this.org = org;
@ManyToOne
@JoinColumn(name="org_id")
public Org getOrg()
return org;
以上就是annotation强大的地方
不过就是修改没有配置文件的方式方便
使用 hibernate、hibernate 注释和 ehcache 的 Maven 依赖项是啥?
【中文标题】使用 hibernate、hibernate 注释和 ehcache 的 Maven 依赖项是啥?【英文标题】:What are the Maven dependencies to use hibernate, hibernate annotations, and ehcache?使用 hibernate、hibernate 注释和 ehcache 的 Maven 依赖项是什么? 【发布时间】:2011-05-04 19:14:14 【问题描述】:我想用最新的 hibernate、hibernate-annotations 和 ehcache 依赖项更新我的 Maven pom.xml。
我阅读了休眠下载页面和 ehcache 下载页面。我所有试图解释它的尝试似乎都失败了。有人可以写出应该进入 pom.xml 的 sn-p 吗?
神秘的休眠下载页面: http://hibernate.org/downloads
神秘的 ehcache 下载页面: http://www.terracotta.org/dl/ehcache-oss-download-catalog
这是我目前所拥有的:
<properties>
<hibernate.version>3.6.0.Final</hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>$hibernate.version</version>
<exclusions>
<exclusion>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
</exclusion>
<exclusion>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>$hibernate.version</version>
<exclusions>
<exclusion>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.2.0</version>
<type>pom</type>
</dependency>
</dependencies>
<repositories>
<repository>
<id>codehaus-repository</id>
<name>Codehaus</name>
<url>https://nexus.codehaus.org/content/repositories/releases/</url>
<layout>default</layout>
</repository>
<repository>
<id>java.net</id>
<url>http://download.java.net/maven/1</url>
<layout>legacy</layout>
</repository>
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>sonatype-ehcache</id>
<url>http://oss.sonatype.org/content/repositories/sourceforge-releases</url>
<layout>default</layout>
</repository>
<repository>
<id>JBoss Repository</id>
<url>https://repository.jboss.org/nexus/content/groups/public</url>
<layout>default</layout>
</repository>
</repositories>
我知道,它看起来很乱,因为我似乎不知道所有依赖项都存储在哪里。
以上失败:
Invocation of init method failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
更新: 帕斯卡,你的回复很清楚,解释得很好。但是,hibernate 在启动时仍然失败。
sl4j 抛出错误,添加:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
这个警告有很多实例:
g in the two-phase commit!
1797 [main] WARN net.sf.ehcache.hibernate.AbstractEhcacheRegionFactory - No Tr
ansactionManagerLookup found in Hibernate config, XA Caches will be participatin
另外,这个错误会让一切都失败:
相关原因:
org.springframework.beans.factory.BeanCreationException: Error creating bean wit
h name 'financialDAO' defined in ServletContext resource [/WEB-INF/config/applic
ationContext-database.xml]: Cannot resolve reference to bean 'transactionManager
' while setting bean property 'transactionManager'; nested exception is org.spri
ngframework.beans.factory.BeanCreationException: Error creating bean with name '
transactionManager' defined in ServletContext resource [/WEB-INF/config/applicat
ionContext-database.xml]: Cannot resolve reference to bean 'sessionFactory' whil
e setting bean property 'sessionFactory'; nested exception is org.springframewor
k.beans.factory.BeanCreationException: Error creating bean with name 'sessionFac
tory' defined in ServletContext resource [/WEB-INF/config/applicationContext-dat
abase.xml]: Invocation of init method failed; nested exception is java.lang.Null
PointerException
at org.springframework.beans.factory.support.BeanDefinitionValueResolver
.resolveReference(BeanDefinitionValueResolver.java:328)
确认一下,这是spring配置:
<!-- Enable EHCache -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">/ehcache.xml</prop>
更新: 这似乎是休眠卡住的最新异常:
Caused by: java.lang.NullPointerException
at net.sf.ehcache.hibernate.HibernateUtil.loadAndCorrectConfiguration(HibernateUtil.java:48)
at net.sf.ehcache.hibernate.EhCacheRegionFactory.start(EhCacheRegionFactory.java:79)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:236)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1842)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:855)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:774)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
【问题讨论】:
暂时不用你的 ehcache.xml 试试(即从你的 spring 配置中删除它)。 当我删除 hibernateproperties 中的所有 ehcache 条目时,应用程序启动正常。当我只删除 configurationResourceName 条目时,它表示它正在使用故障转移配置。 【参考方案1】:如果您真的指的是 hibernate-annotations(而不是 hibernate-entitymanager)和 ehcache 的终极版本,那么您将需要以下内容:
<project>
...
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.0.Final</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.2.0</version>
</dependency>
...
</dependencies>
<repositories>
<repository>
<id>repository.jboss.org-public</id>
<name>JBoss repository</name>
<url>https://repository.jboss.org/nexus/content/groups/public</url>
</repository>
</repositories>
...
</project>
从 Hibernate 3.6 开始,JDK 1.4 支持已被删除,Hibernate Annotations 已合并回 Core。
我的意思是 Hibernate EntityManager,然后在上面的 sn-p 中将 hibernate-core
替换为 hibernate-entitymanager
。
关于 EHCache,ehcache-core
应该提供您需要的一切(包括“新”Hibernate 3.3/3.5 caching SPI 及其CacheRegionFactory
的实现)。所以,作为documented:
Setting Ehcache as the Hibernate Second Level Cache
休眠 3.3 及更高版本
注意休眠 3.2 用户
请务必注意属性名称和值的更改。
用途:
<property name="hibernate.cache.region.factory_class"> net.sf.ehcache.hibernate.EhCacheRegionFactory</property>
例如创建实例,或
<property name="hibernate.cache.region.factory_class"> net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</property>
强制 Hibernate 使用单例 Ehcache CacheManager.
跟进:回答您的更新:
sl4j 抛出错误,通过添加 (...)
好吧,我猜不出您使用的是什么日志框架,提供您选择的绑定作为练习留给读者 :)
这个警告有很多实例(...)
在非 JTA 环境中运行时,我只得到一个 WARNING
。如果这是您的情况(非 JTA 环境),我会认为 WARNING
正常。如果这不是您的情况,请查看有关 JTA 的文档。
另外,这个错误会让一切都失败
这是一个不同的问题(但看起来SessionFactory
无法正确初始化,激活日志以查看原因),我建议发布一个新的春季特定问题。
【讨论】:
评论框里的格式太可怕了,会更新上面的问题。 帕斯卡,再次感谢您的帮助。你看起来很犀利。而且,针对配置问题提出一个新问题可能是有意义的。【参考方案2】:如果你将 Spring 3 与 hibernate 3 集成,你可以通过这个 pom.xml
<properties>
<org.springframework.version>3.0.5.RELEASE</org.springframework.version>
</properties>
<dependencies>
<!-- Core utilities used by other modules. Define this if you use Spring
Utility APIs (org.springframework.core.*/org.springframework.util.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>$org.springframework.version</version>
</dependency>
<!-- Expression Language (depends on spring-core) Define this if you use
Spring Expression APIs (org.springframework.expression.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>$org.springframework.version</version>
</dependency>
<!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define
this if you use Spring Bean APIs (org.springframework.beans.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>$org.springframework.version</version>
</dependency>
<!-- Aspect Oriented Programming (AOP) Framework (depends on spring-core,
spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>$org.springframework.version</version>
</dependency>
<!-- Application Context (depends on spring-core, spring-expression, spring-aop,
spring-beans) This is the central artifact for Spring's Dependency Injection
Container and is generally always defined -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>$org.springframework.version</version>
</dependency>
<!-- Various Application Context utilities, including EhCache, JavaMail,
Quartz, and Freemarker integration Define this if you need any of these integrations -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>$org.springframework.version</version>
</dependency>
<!-- Transaction Management Abstraction (depends on spring-core, spring-beans,
spring-aop, spring-context) Define this if you use Spring Transactions or
DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>$org.springframework.version</version>
</dependency>
<!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context,
spring-tx) Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>$org.springframework.version</version>
</dependency>
<!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA,
and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx)
Define this if you need ORM (org.springframework.orm.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>$org.springframework.version</version>
</dependency>
<!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB,
JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans,
spring-context) Define this if you need OXM (org.springframework.oxm.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>$org.springframework.version</version>
</dependency>
<!-- Web application development utilities applicable to both Servlet and
Portlet Environments (depends on spring-core, spring-beans, spring-context)
Define this if you use Spring MVC, or wish to use Struts, JSF, or another
web framework with Spring (org.springframework.web.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>$org.springframework.version</version>
</dependency>
<!-- Spring MVC for Servlet Environments (depends on spring-core, spring-beans,
spring-context, spring-web) Define this if you use Spring MVC with a Servlet
Container such as Apache Tomcat (org.springframework.web.servlet.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>$org.springframework.version</version>
</dependency>
<!-- Spring MVC for Portlet Environments (depends on spring-core, spring-beans,
spring-context, spring-web) Define this if you use Spring MVC with a Portlet
Container (org.springframework.web.portlet.*) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>$org.springframework.version</version>
</dependency>
<!-- Support for testing Spring applications with tools such as JUnit and
TestNG This artifact is generally always defined with a 'test' scope for
the integration testing framework and unit testing stubs -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>$org.springframework.version</version>
<scope>test</scope>
</dependency>
<!-- Hibernate resources -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.3.0.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.1.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
<!-- EhCache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
</dependency>
<!-- Taglibs -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>20030825.184428</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>20030825.183949</version>
</dependency>
<!-- Log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<type>jar</type>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
如果你想要不同版本的hibernate,你可以查看here
【讨论】:
【参考方案3】:<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.2.Final</version>
</dependency>
【讨论】:
请编辑更多信息。不建议使用纯代码和“试试这个”的答案,因为它们不包含可搜索的内容,也没有解释为什么有人应该“试试这个”。以上是关于hibernate中的annotations是啥意思的主要内容,如果未能解决你的问题,请参考以下文章
the hash for the file is not present in the specified catalog file,是啥意
使用 hibernate、hibernate 注释和 ehcache 的 Maven 依赖项是啥?
hibernate中使用annotation映射的时候,如何指定实体类中的某些字段不映射到数据库?