Sunday, November 7, 2010

How To Solve Tally C0000005 Error

JPA with Spring, Hibernate JBoss

In this post I will explain the easiest way to use JPA with Spring in a web application that uses Struts2 like MVC framework.

Obviously in my case will extend Struts2Tutorial which if you read my previous tutorial about these technologies, you should know that I put in and attached to the post with the new implementations ...

will add a new contact management service that is providing the usual operations of insertion, searching, editing and deleting (aka CRUD) of a person and his contact details.

I will not go into technical, but see the steps to configure and use Spring with JPA on JBoss I'll assume some knowledge on these frameworks.

Before you configure the development environment.

Since the use of JPA and the transactions I had to use an application server that supports transactional without any configuration. JBOSS 4.2.2 GA so I chose my tutorial is based on the latter but you can choose any another. Fine

hours be sure to configure your project so that it can be compiled with the default profile jboss libraries.

WEB-INF/lib folder of the web project must be present the following libraries:

  • aopalliance.jar
  • commons-beanutils-1.7.0.jar commons-digester-
  • 2.0.jar commons-fileupload-1.2.1.jar
  • commons -logging-1.0.4.jar dom4j-1.6.1.jar
  • Freemarker-2.3.15.jar log4j-1.2.15.jar
  • mysql-connector-java-5.1.12-bin.jar
  • OGNL-2.7.3.jar slf4j-api-1.5.8.jar slf4j-
  • log4j12-1.5.0.jar spring-
  • aop.jar
  • spring-beans-2.5.6.jar
  • spring-context-2.5.6.jar
  • spring-core-2.5.6.jar spring-
  • jdbc.jar
  • spring-spring-orm.jar
  • tx.jar
  • spring-web-2.5.6.jar
  • Struts2-core-2.1.8.1.jar
  • Struts2-spring-plugin-2.1.8.1.jar
  • Struts2-tiles-plugin-2.1.8.1.jar
  • tiles-api-2.0.6.jar tiles-core
  • -2.0.6.jar tiles-jsp-2.0.6.jar
  • xwork-core-2.1.6-modified.jar
If you notice the ' Last library there is a modified because I had to change it for its incompatibility with jboss. However found the reason and the change in my previous post:

JBoss, Hibernate, JPA, Spring, Struts2: Javassist Enhancement failed



configured the working environment we pass to development.

The first thing to do is configure this integration ... As you know

JPA uses its own XML configuration file called persistence.xml then create a folder META-INF the root of the source (src / META-INF) and created inside the persistence.xml file with following content:



as you can see in our case, we leave it blank without any configuration to give only the name persistence-unit . We will make sure that spring to configure JPA with its configuration files.

Configure Now here is the spring ' applicationContext.xml necessary: \u200b\u200b



Then we configured in Spring XML as a bean dataSource which will handle communication with the db connection and instantiating the class of Spring DriverManagerDataSource and providing the necessary parameters. We

Defne another bean named EntityManagerFactory that instantiates a class of type LocalContainerEntityManagerFactoryBean . Such is the easiest to use Spring but it provides more depending on the architecture of your software and transaction management, you have chosen.
A bean that we have set the datasource previously defined. We told him what
jpaVendorAdapter used in our If a type HibernateJpaVendorAdapter because we use Hibernate as ORM. In the parameters of
jpaVendorAdapter we told him the type of database (MySQL), to automatically generate the bill and see the sql logs. As a last property of
EntityManagerFactory we have set jpaProperties setting the dialect to use and generate the necessary tables and delete db on each run and stop the application server. Of course I used this as a convenience so you do not have to rebuild the db tt for any modification;), but if you do not just put it as the parameter value update . With

we told Spring that will be defined by the transactional annotation classes.

In the end we have defined the type of bean TransactionManager JpaTransactionManager that handles the transaction, saying that is the ' EntityManagerFactory namely that you created earlier.

As you can see tt the configuration file is in the JPA configuration of Spring. The rest you see in the file does not center with JPA, but if you want to know what good look at my previous post.

Finally to complete the setup we have to insert the filter OpenEntityManagerInViewFilter in web.xml

The filter is used to map to which requests must instantiate the Spring entityManager. Here is the updated web.xml content:




Now for the data model and from now on, given that the aim was to show how to integrate JPA with Spring, I will be less descriptive, except for several precautions.
The data that we will manage are Person and Address. Where a person can have multiple addresses. Here are the classes:

Persona.java


As you can see is a simple Pojo whose attributes id, name, and a list of addresses. We used to map the location of the JPA annotations, I'll go further to a general review the official documentation: @ Entity
  • we told him that is on a separate db and in this case (not spqcificando table) will be created or otherwise mapped to a table with name Person.
  • @ Id we said that the attribute is the primary key.
  • @ GeneratedValue used to define the strategy for the generation of id in our case car, that will generate them in the db.
  • @ Column indicates that the attribute is a field in the table
  • @ OneToMany we have defined a one to many association.
  • @ JoinTable used to say that the association will use a table join
Recapito.java


There are the same except annotation @ ManyToOne indicating a one to many association and joinTable pointing to the same table join pima.

The result of the model data to db is as follows:


Defined model PersonaDao pass the class. Here's the interface:


then the interface provides the following methods:
  • save or change a person
  • removal of a person looking for a person
  • recovery of all persons
  • adding an address to a Perona
  • remozione an address
Given the simplicity of the model I created for a single dao Person for delivery. And here is its implementation
PersonaDaoImpl:


Before describing another look at the rows from 29 to 34 we defined the entityManager and its set method which will be used by Spring to set the entityManager in the configuration file . This setting will be done by Spring as we put on the method, the annotation @ PersistenceContext .

careful to line 25 then there is the annotation @ Transactional . M ettendola like this case as an annotation of class we told Spring that any method defined in it is transactional. That is, each transaction starts and ends and out of respect to the invocation method. But we can also define a method-level transaction using the annotation as annotation method.
In any case it is said that the transactional is defined in the dao, but it can also be defined in a class that calls the dao (liquids at even higher) in a class that acts as a business-logic or domain of the service.

As you can see the entity manager provides access methods to read and write to db as persist (new object), merge (update an object), find (reading an object) to select CreateQuery details etc. ... but there are others.

last thing to pay attention to the implementation of methods that access in writing. Make sure that the first well to continue or modify an object that is fully consistent with all other objects that it has as attributes. Now here's the

ContattiBl ie the class which is the domain or business logic of the service contacts and urilizza inside PersonaDao:


In the project I've used it recalled in Struts2 action but this part of the little man you now should be in control of thanks to my Struts2 tutorial: P You can download the project here without a library.

0 comments:

Post a Comment