theapplicationbox.com's blog discussing JEE technologies and web related experiences
Tuesday, March 19, 2013
Use "hibernate.hbm2ddl.auto=update" to update your schema (add remove columns, etc.) without losing data in tables.
Use <property name="hibernate.hbm2ddl.auto" value="update" />
to update your schema (add remove columns, etc.) without losing data in tables.
You can now freely add remove fields in your Java entities and on next run of your program, Hibenate will update the tables without droping them (in contrast to "create", see Hibernate docs for possible values)
Wonderful!
Friday, March 8, 2013
Hibernate Compatibility Matrix
If you want to use JPA 1.0 and Hibernate, see
https://community.jboss.org/wiki/HibernateCompatibilityMatrix?_sscc=t
You need for example:
- Hibernate 3.3.2 GA:
hibernate3.jar
+(from lib/bytecode:)
javassist-3.9.0.GA.jar
+(from lib/required)
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
jta-1.1.jar
slf4j-api-1.5.8.jar
and
Hibernate 3.4.0.GA (entity manager)
hibernate-entitymanager.jar
ejb3-persistence.jar
and
Hibernate 3.4.0.GA annotations
hibernate-commons-annotations.jar
hibernate-annotations.jar
For JPA 2 (meaning Hibernate greater than 3.5)
the docs says:
Please note that as of 3.5.x Hibernate Core, Hibernate Annotations and Hibernate EntityManager are all versioned and released together which greatly simplifies this matrix; see this blog for details.
Hibernate Compatibility Matrix
Created on: Apr 23, 2010 3:01 PM by Brad Maxwell - Last Modified: Jul 22, 2012 8:29 PM by Ondrej Zizka
Please note that as of 3.5.x Hibernate Core, Hibernate Annotations and Hibernate EntityManager are all versioned and released together which greatly simplifies this matrix; see this blog for details.
Hibernate Commons Annotations is a utility project used by annotations based sub-projects like Hibernate Search, and thus maintained as a separate project. It is a compile time dependency for Hibernate Core 3.6.0 and up.
| Package | Version | Core | Annotations | EntityManager | Validator | Search | Shards | Tools |
|---|---|---|---|---|---|---|---|---|
Hibernate Core
| 3.2.6 GA | - | 3.2.x, 3.3.x | 3.2.x, 3.3.x | 3.0.x | 3.0.x | 3.0.x | 3.2.x |
| 3.3.2 GA | - | 3.4.x | 3.4.x | 3.1.x | 3.1.x | Not compatible | Not compatible | |
Hibernate
Annotations
| 3.3.1 GA | 3.2.x | - | 3.3.x | 3.0.x | 3.0.x | 3.0.x | 3.2.x |
| 3.4.0 GA | 3.3.x | - | 3.4.x | 3.1.x | 3.1.x | Not compatible | Not compatible | |
Hibernate
EntityManager
| 3.3.2 GA | 3.2.x | 3.3.x | - | 3.0.x | 3.0.x | Not compatible | 3.2.x |
| 3.4.0 GA | 3.3.x | 3.4.x | - | 3.1.x | 3.1.x | Not compatible | Not compatible | |
Hibernate
Validator
| 3.0.0 GA | 3.2.x | 3.3.x | 3.3.x | - | 3.0.x | 3.0.x | 3.2.x |
| 3.1.0 GA | 3.3.x | 3.4.x | 3.4.x | - | 3.1.x | Not compatible | Not compatible | |
Hibernate
Search
| 3.0.1 GA |
>= 3.2.2
(better if >= 3.2.6)
|
3.3.x
(better if >= 3.3.1 )
| 3.3.x | 3.0.x | - | 3.0.x | (3.2.x) |
| 3.1.1 GA | 3.3 | 3.4 | 3.4 | 3.1 | - | Not tested | (3.2.x) | |
Hibernate
Shards
| 3.0 Beta2 | 3.2.x | 3.3.x | Not compatible | 3.0.x | 3.0.x | - | - |
Hibernate
Tools
| 3.2.2 | 3.2.x | 3.2.x and 3.3.x | 3.2.x and 3.3.x | 3.0.x | (3.2.0) | - | - |
Tuesday, March 5, 2013
Co-existence with Hibernate, JPA, and EJB3
Very interesting
1. How to use JPA entities with standard Hibernate Session:
+ use Hibernate's AnnotationConfiguration class or Spring's org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
2. Or how to configure JPA with hibernate.cfg.xml:
See original post:
Co-existence with Hibernate, JPA, and EJB3
http://bill.burkecentral.com/2007/07/06/co-existence-with-hibernate-jpa-and-ejb3/
Also see javadoc for Spring's AnnotationSessionFactoryBean
org.springframework.orm.hibernate3.annotation
1. How to use JPA entities with standard Hibernate Session:
<hibernate-configuration>
<session-factory>
<mapping class="com.acme.Flight"/>
<mapping class="org.jboss.Sky"/>
<mapping resource="org.acme.orm.xml"/>
</session-factory>
</hibernate-configuration>
+ use Hibernate's AnnotationConfiguration class or Spring's org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
2. Or how to configure JPA with hibernate.cfg.xml:
<properties>
<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/>
</properties>
See original post:
Co-existence with Hibernate, JPA, and EJB3
http://bill.burkecentral.com/2007/07/06/co-existence-with-hibernate-jpa-and-ejb3/
Co-existence with Hibernate, JPA, and EJB3
July 6, 2007
11 Votes
How can you slowly migrate existing Hibernate apps to JPA? How can you use Hibernate features and unique mapping capabilities with a JPA deployment? How can you take advantage of EJB3 injection and persistence context management with Hibernate? In this blog I want to show how Hibernate, JPA, and EJB 3.0 can co-exist and compliment one another. This information is fully documented between the Hibernate and JBoss EJB3 projects, but I thought I’d highlight them in this blog to make the community aware that they exist.
Mixing and matching JPA annotations within Hibernate
Using the standard JPA annotations helps out greatly in cutting down the amount of XML metadata you have to type. This annotation metadata is an exact replacement for much of the metadata in hbm.xml. Using them does not require you to use the JPA EntityManager interface. You may still use SessionFactory and Session objects to interact with your database. After downing the Hibernate Anotations project, setting things up is fairly easy:
SessionFactory sessionFactory =
new AnnotationConfiguration().buildSessionFactory();
You use hibernate.cfg.xml to specify which exact classes you want mapped by this SessionFactory:
<hibernate-configuration> <session-factory> <mapping class="com.acme.Flight"/> <mapping class="org.jboss.Sky"/> <mapping resource="org.acme.orm.xml"/> </session-factory> </hibernate-configuration>
You can mix hbm.xml mapping files with new annotated classes. XML resources can also either be JPA-based XML or Hibernate based. This mixing and matching allows you to quickly prototype new mappings using JPA, but allows these mappings to co-exist with older Hibernate 3 applications.
There are a few more options for configuration. Check out the Hibernate Annotationsdocumentation for more information.
Use Hibernate to configure JPA
If you want to use the JPA Entity Manager API to code a portable application, you may still want to use Hibernate metadata to map your beans. Although the JPA orm mapping is pretty rich, it is still a subset of Hibernate’s functionality. The Hibernate Annotations project provides additional Hibernate-specific annotations to elaborate a persistence mapping where JPA leaves off. If you prefer XML, you can use hbm.xml files to define the mappings for your entity beans. When loading a persistence archive, Hibernate Entity Manager will automatically scan the .jar file for any *hbm.xml files and add them to its mapping information.
Hibernate specific configuration is defined in the properties element of the persistence.xml file. You can configure any hibernate property within this XML blob
<persistence>
<persistence-unit name="manager1" transaction-type="JTA">
<jta-data-source>java:/DefaultDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
</persistence-unit>
</persistence>
You may also define all your hibernate configuration in the usual Hibernate way: within a hibernate.xfg.xml file. You must tell the JPA implementation to use this configuration file through the hibernate.ejb.cfgfile property.
<persistence> <persistence-unit name="manager1" transaction-type="JTA"> <jta-data-source>java:/DefaultDS</jta-data-source> <properties> <property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/> </properties> </persistence-unit> </persistence>
Also see javadoc for Spring's AnnotationSessionFactoryBean
http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html
org.springframework.orm.hibernate3.annotation
Class AnnotationSessionFactoryBean
java.lang.Objectorg.springframework.orm.hibernate3.AbstractSessionFactoryBean
org.springframework.orm.hibernate3.LocalSessionFactoryBean
org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
- All Implemented Interfaces:
- BeanClassLoaderAware, DisposableBean, FactoryBean, InitializingBean, ResourceLoaderAware, PersistenceExceptionTranslator
public class AnnotationSessionFactoryBean
- extends LocalSessionFactoryBean
- implements ResourceLoaderAware
Subclass of Spring's standard LocalSessionFactoryBean for Hibernate, supporting JDK 1.5+ annotation metadata for mappings.
Note: This class requires Hibernate 3.2 or higher, with the Java Persistence API and the Hibernate Annotations add-on present.
Example for an AnnotationSessionFactoryBean bean definition:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="annotatedClasses"> <list> <value>test.package.Foo</value> <value>test.package.Bar</value> </list> </property> </bean>Or when using classpath scanning for autodetection of entity classes:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="test.package"/> </bean>
Subscribe to:
Posts (Atom)
Use "hibernate.hbm2ddl.auto=update" to update your schema (add remove columns, etc.) without losing data in tables.
Use <property name="hibernate.hbm2ddl.auto" value="update" /> to update your schema (add remove columns, etc...
-
In SVN and Eclipse, if you have problems with a folder, with a small red x, try right-click->Team->Revert and then right-click->Te...
-
Use <property name="hibernate.hbm2ddl.auto" value="update" /> to update your schema (add remove columns, etc...
-
Read this: Primary Keys through OneToOne and ManyToOne Relationships and this Derived Identifiers and this Derived Identi...
