• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple JPA Entity with NUMBER

 
joseph dmello
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 38
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please try using dialect property in persistence.xml under properties tag
 
joseph dmello
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 38
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 38
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not create the table.
Let Hibernate create it by itself.

Also alternatively did you tried changing datatype to int.
 
joseph dmello
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic