Wednesday, February 26, 2014

Resolved Error: Class has two properties of the same name this problem is related to the following location

I have created a bean which I will return in a web service. When deploying on JBoss Eap 6, I have detected this error:

 Class has two properties of the same name  this problem is related to the following location

The source of the problem is that I have both XmlAccessType.FIELD and getters and setters.
The solution is then to create constructors (the default one and with all fields) and remove all the setters. Now it is working.

This is not a good solution !!
So what I have recently discovered thanks to a friend (Ridwan), is that I was putting the attributes of the bean as PUBLIC and having also the getter. In fact, for a bean, the attributes should be PRIVATE (If you don't put the visibility it will be private by default).
The nice Solution is thus to do:

Class Car{

   private String model;
  
   public Car(){
        super();
   }

   public String getModel(){
       return this.model;
   }

   public void setModel(String model){
      this.model =  model;
   }
   
}

Tuesday, February 25, 2014

Resolve Error: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Working in a JPA project, I had this error:

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
....
Caused by: org.postgresql.util.PSQLException: ERROR: relation .. does not exist

This error can appear if you are working with two schema in your database. If you are declaring a persistence.xml with only one persistence-unit, there will be an ambiguity as Hibernate will not know the schema on which you are working.
So to resolve this, one possible solution is to add schema name in you entity.

@Entity
@Table(name = "my_table", schema="schema_one")
public class MyTable implements java.io.Serializable

Resolve Error :java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: is not mapped

This is a nice error that has take a good time.
By searching this error in the internet, the problem can be about the Entity name given in the query. If you are using HQL, you should specify the Entity name and not the Table name.

But my problem is beyond that. In fact I have two errors:
1- If I specify the table name in an sql query I will have

Internal Exception: org.postgresql.util.PSQLException: ERROR: relation ... does not exist


2- If I specify the Entity name, I will have
java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: is not mapped

So I have suspected that my entities are not mapped from the database. In fact even if I open the Hibernate perspective and see my project, I have an error. See this link.
As a solution, I have fixed my persistence.xml, make sure that I can access the tables from the Hibernate console, and thus generate my Entities.

I will sooner post a new article about how to work with JBoss eap6.2 as I have discovered many problems. 

Resolve a org.hibernate.HibernateException: Wrong column type in Error

While generating Entities, some problems can occur.  In fact in some Entities, I had this error:

org.hibernate.HibernateException: Wrong column type Found: int2, expected: int4

In my database, I was using smallint. But when the entities were generated, the attrbute type was set to Integer.

As I need smallint type in my database, the solution was to modify the attribute type to short.

This is sufficient to resolve the problem.

Resolve [Classpath]: Could not create JPA based Configuration and Invalid persistence xml Errors in Eclipse JBoss eap 6.2

The problem is that when I have created the persistence.xml, I was using JPA2.1. JBoss Eap 6.2 comes with JPA2.0, I am also using JBoss Tools. So I have two problems

by: javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML (line-1 : column -1): cvc-elt.1: Cannot find the declaration of element 'persistence'.

and 

[Classpath]: Could not create JPA based Configuration






Solution:  You should not use JPA2.1 but instead use JPA 2.0 as JBoss comes with this latest version. So in the persistence.xml use this declaration.

persistence version="2.0" 
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"

Now my persistence.xml is working and I can see my tables in the Hibernate View.


And Here my Hibernate console:

Don't forget to specify the Database connection with the right JDBC. after this, you can see your tables. And thus generate your entities.

Right clic on the project --> JPA tools --> Generate Entities from Tables, and Specify the Hibernate Console.


The entities will be correctly generated.


Monday, February 24, 2014

Resolved Error: org.hibernate.MappingException: Repeated column in mapping for entity

When creating a JPA project, I have created my entities using eclipse and Hibernate tools, See this link.When deploying my application on JBoss server, I had this error:

 org.hibernate.MappingException: Repeated column in mapping for entity: org.qmic.mdd.enterprise.model.MddRelationTableOneTableTwo column: person_id (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:696)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:718)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:740)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:493)
at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1324)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1786)
at org.hibernate.ejb.EntityManagerFactoryImpl.(EntityManagerFactoryImpl.java:96)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)


In fact, in some association table I use a Composite Primary Key. As the association table should only use these keys, the association with these keys should be set to insert=false and update=false. It is not of the relation class to modify these attributes.

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="person_id", insertable=false, updatable=false)

So adding these properties, the problem is resolved.


Thursday, February 20, 2014

JBoss EAP 6 Persistence unit problem: Jboss specify the unitName for each reference to a persistence unit

When depolying my application on JBoss EAP 6.2, I had this error:

"
 Persistence unitName was not specified and there are 2 persistence unit definitions in application deployment deployment "myapp.war".  Either change the application deployment to have only one persistence unit definition or specify the unitName for each reference to a persistence unit.
"
My application uses JPA 2.1 with Hibernate implementation 4.3. My persistence.xml contains two persistence-unit, one per DB schema. See my JPA example.



NB: with Hibernate 4.3, the provider is org.hibernate.jpa.HibernatePersistenceProvider.
JBoss (eap 6.x, as 7.x) it implicitly load some modules. JPA will also be loaded by JBoss as it detects the persistence.xml. According to the dosumentation :

"During application deployment, JPA use is detected (e.g. persistence.xml or @PersistenceContext/Unit annotations) and injects Hibernate dependencies into the application deployment. This makes it easy to deploy JPA applications."


JBoss eap 6.2 comes also with JPA 2.0 and Hibernate 4.2.7. More details about the module version is avialable in this RedHat Link


In order to remove this error while deploying, and as I provide the required libs. I have edited the standalone.xml (Shutdown JBoss first) by removing the JPA subsystem subsystem xmlns="urn:jboss:domain:jpa:1.1".




When starting JBoss, this subsytem definition will be removed even from standalone.xml.

Now my application is deploying on JBoss.

Mmmmm I have found a better think. In fact, as I am defining two persistence-unit, every time that I use @PersistenceUnit or @PersistenceContext, I should specify the unitName, otherwhise the created entityManager will not know on which persistenceUnit it is working.
After a search in all my classes, I have found that really I have forgot to mention that. So I have restored the JPA subsystem in JBoss (standalone.xml). Now everytime that I use @Persistencexxx, I specify the  unitName.

@PersistenceContext(unitName="my_first_persitence_unit") 
@PersistenceUnit(unitName="my_second_persitence_unit") 





Tuesday, February 18, 2014

Tutorial Java Persistence Api with Hibernate4, Spring 4 and Jboss eap 6.2 (Entity generation)


Today we will work on the creation of a web project with these properties:
- Server JBoss eap6.2
- Database Postgres 9.2
- Relational mapping : Use of Hibernate 4
- Spring 4
- Maven

Once my database is designed, comes the choice of the ORM. I have chosen the JPA specification with Hibernate implementation.

Step1: create a dynamic web project 



Convert the project to Maven project. Then use the same steps to convert it to JPA project.


Select the version of JPA and Java.


Be sure that Hibernate (JPA 2.1) is selected.


After downloading Spring tools in eclipse, you can add Spring nature to the project:



Step 2: Configure the JBoss EAP 6.2 server 

Now we will add a server on which our application will run. So let's use the JBoss EAP 6.2


If you don't have an environment runtime, you can configure it by specifying the your server directory



Make sure that you are specifying the right JDK


I have also downloaded JBoss eap 6.2 repository as I am using Maven.
I am using JBoss which comes with Hibernate, JPA..., so I will use these modules as JBoss load them implicitly.
My initial pom.xml looks like this 





Step3: Datasource Configuration

Now that the server is created, we will configure the datasource. This is no more done in separate file *-ds.xml as in previous versions of JBOss. It is done in the  C:\EAP-6.2.0\jboss-eap-6.2\standalone\configuration\standalone.xml file. 


You should add the JDBC Dirver (for me it is postgresql-9.2-1004.jdbc4.jar) in JBoss modules. So create this folder hierarchy: org\postgresql\main inside JBoss modules folder: C:\EAP-6.2.0\jboss-eap-6.2\modules\system\layers\base\. Copy then your driver and create module.xml



Step4: Setting the persistence.xml

Now we will configure the persistence.xml setting file located under src/META-INF folder 
JPA implementations allow the developer to chose between a JTA or RESOURCE_LOCAL transaction management.
The Java Transaction API (JEE APi ) enables distributed transactions to be done across multiple resources in Java. JBoss server supports JTA transactions.
RESOURCE_LOCAL is using basic JDBC level transactions, thus it is not possible to span a transaction among multiple persistence unit.


Once created, use Window-> Open perspective -> Others -> Hibernate. If every think is weel done, you can see table in your DB




Step 5: Fix the connection with the Database.

Now go to window -> Perspective -> Hibernate. The defined datasource will be there. Double clic on it. I have and error: 
"org.Hibernate.console.HibernateConsoleRuntimeException: Could not create JPA based Configuration"

In fact I think I should configure the connection by specifying my Driver ... So rigth clic on the configuration and edit it.






We should specify the driver. By default eclipse comes with version 8. As my database is 9.2, I should remove that driver and add postgres9.2.


Specify the Driver location. Make sure then that the properties are well specified.


After adding the driver, properties, press OK. You can then validate the connection.



It seems that there is a problem in eclipse with JPA project and Hibernate tools. So we cannot visulaize our tables. 
EDIT: See this link to Resolve this Classpath problem and generate tables.
Don't worry, I have found the solution ;) Great.
Ok so now in order to generate tables.





Once the tables selected, you can go ehead in the creation and entities will be ready for use.

Soon I will finish this tutorial with spring config....

Step 5: Spring


So first thing to do is to add maven dependency to spring-orm. Next create the spring configuration file,  applicationContext.xml.


Once the name specified, you can chose the XSD namespaces which will be used and their versions.



According to the Spring 4 documentation "Using the LocalContainerEntityManagerFactoryBean" is the most powerful JPA setup option, allowing for flexible local configuration within the application. In fact, using org.springframework.orm.jpa.LocalEntityManagerFactoryBean, developer isn't able to refer to an existing JDBC Datasource bean definition neither support global transactions.
so finally, my ApplicationContext.xml is:





Hibernate JPA 2.1 Use multiple database schemas : One database, two schemas

I was working with JPA2.1, Hibernate implementation when I have figured a problem. I have a database with two different schema.

Step 1: Remove validation

If JPA validation is not removed, you will get errors


Step 2:  Define persistence.xml

In the persistence.xml define two persistence-unit each for one schema



Step 3: Define the mapping file

Now we define the two mapping files with schema name.


Step4: Create Entities


Once the entities created, make sure that the class description is located in the right persistence-unit. I have seen that generated with eclipse, by default all classes description will be in the first persistence-unit. 

In the entity itself, you can add also the schema name (You can use the auto-completion as eclispe recognize the two schemas)

@Table(name="myTable", schema="myschema1")

Step 5: Specify the unitName

Every time that you use @PersistenceContext or @PersistenceUnit annotation in your code, you should specify the persistence unit name that you are using.

@PersistenceContext(unitName="urm_mdd_module_schema")


Articles les plus consultés