GeeCON Prague 2014*
The moose likes Object Relational Mapping and the fly likes Simple JPA Entity with NUMBER Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Simple JPA Entity with NUMBER" Watch "Simple JPA Entity with NUMBER" New topic
Author

Simple JPA Entity with NUMBER

joseph dmello
Greenhorn

Joined: Jan 12, 2013
Posts: 6
Hi, I've been trying some very simple JPA and I can't figure out use of simple NUMBER for an oracle database.
Could anyone please tell me what's going wrong?

I create my POJO entity like this





My persistence.xml:


And the method I'm using to create an entity:



I'm using an Oracle database and I create the table using:

CREATE TABLE test (
userId NUMBER(6),
username VARCHAR2(50),
userPassword VARCHAR2(50),
userRole VARCHAR2(50),
CONSTRAINT test_pk PRIMARY KEY (userId)
);

When I set userId to VARCHAR2 and my variables to String type its fine, its just with NUMBER that I'm stuck.
It seems its such a basic problem that I'm having trouble finding a solution. The output I get is:

1
Provided id of the wrong type for class TestEntity. Expected: class java.lang.Integer, got class java.lang.String

I'm sure its a simple solution but I'm afraid I'm at a loss. Any help greatly appreciated!
Vijay Vishwa
Greenhorn

Joined: Nov 21, 2010
Posts: 28
Please try using dialect property in persistence.xml under properties tag


SCJP 5.0 OCBCD 5.0(Formerly SCBCD)
joseph dmello
Greenhorn

Joined: Jan 12, 2013
Posts: 6
Vijay Vishwa wrote:Please try using dialect property in persistence.xml under properties tag

Thanks for the reply. Unfortunately I still seem to be having problems.

I got error:
SEVERE: Servlet.service() for servlet click threw exception
javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML (line-1 : column -1): cvc-complex-type.4: Attribute 'value' must appear on element 'property'.
Error parsing XML (line-1 : column -1): cvc-complex-type.2.1: Element 'property' must have no character or element information item [children], because the type's content type is empty.

After changing the line to
<property name="dialect" value="org.hibernate.dialect.HSQLDialect" />

I received the same error message as before:

Provided id of the wrong type for class TestEntity. Expected: class java.lang.Integer, got class java.lang.String
Vijay Vishwa
Greenhorn

Joined: Nov 21, 2010
Posts: 28
Could you please verify the data type in table.

Also try doing following.

Drop the table.
remveo the earlier dialect propery.
Add follwoing propery in persistence.xml



Dialect depends on which database you use, to brigde the datatype differences.
Also you can try declaring userId datatype as primitive int.
joseph dmello
Greenhorn

Joined: Jan 12, 2013
Posts: 6
Vijay Vishwa wrote:Could you please verify the data type in table.

Also try doing following.

Drop the table.
remveo the earlier dialect propery.
Add follwoing propery in persistence.xml



Dialect depends on which database you use, to brigde the datatype differences.
Also you can try declaring userId datatype as primitive int.


Still no joy I'm afraid.

I reran this script:

DROP TABLE test;

CREATE TABLE test (
userId NUMBER(6),
username VARCHAR2(50),
userPassword VARCHAR2(50),
userRole VARCHAR2(50),
CONSTRAINT test_pk PRIMARY KEY (userId)
);

I added your changes to my persistence.xml and also tried using org.hibernate.dialect.OracleDialect afterwards. I received the same error both times.

I also added
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.format_sql" value="true" />
<property name="use_sql_comments" value="true" />
as they were given in an example but I'm not sure of their purpose.

Still broken
Vijay Vishwa
Greenhorn

Joined: Nov 21, 2010
Posts: 28
Do not create the table.
Let Hibernate create it by itself.

Also alternatively did you tried changing datatype to int.
joseph dmello
Greenhorn

Joined: Jan 12, 2013
Posts: 6
Vijay Vishwa wrote:Do not create the table.
Let Hibernate create it by itself.

Also alternatively did you tried changing datatype to int.


Just tried those suggestions with the same error each time.
When dropping the table it is created no problem but still no data entry is made.
joseph dmello
Greenhorn

Joined: Jan 12, 2013
Posts: 6
I removed my column annotations and changed some of my maven dependencies and it seems this fixed the problem.
Still not sure what exactly caused it but thanks for the help.
 
GeeCON Prague 2014
 
subject: Simple JPA Entity with NUMBER