aspose file tools*
The moose likes Object Relational Mapping and the fly likes Using Oracle sequence with JBOSS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Using Oracle sequence with JBOSS" Watch "Using Oracle sequence with JBOSS" New topic
Author

Using Oracle sequence with JBOSS

Nei San
Greenhorn

Joined: May 08, 2007
Posts: 12
Hi,

I'm trying to use JBOSS IDE for Eclipse but I'm having trouble using an automatic generated id populated from a Oracle sequence previously created in the database. I guess there is some aditional configuration that I need to do in my application but I don't know what it is.

In the persistence XML I have:



At the entity bean I have:



I'm getting the error "java.sql.SQLException: ORA-02289: sequence does not exist".

Thank you.

Nei
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
return id;
}


Try this:



See if it works.


[My Blog] [JavaRanch Journal]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

And most importantly, do you have the Sequence created in your database?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Actually, keep the AUTO in there, but make sure Oracle has a Hibernate Sequence tabled, named "hibernate_seq" or something along those lines. But if you have a hibernate sequence all tables will use that one sequence generator.

If you have a sequence table for each table, then you need another annotation that you put the name of the sequence table to use, but I forget the exact name of that Annotation.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

Originally posted by Mark Spritzler:

If you have a sequence table for each table, then you need another annotation that you put the name of the sequence table to use, but I forget the exact name of that Annotation.





This one?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Originally posted by Jaikiran Pai:




This one?


Nah, I don't think so, so I looked it up and it is






Mark
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

Nei San
Greenhorn

Joined: May 08, 2007
Posts: 12
Hi,

I need an sequence for each table so I changed my code according to your information and worked with the sequence but now I'm getting an error with the foreign key. I have two entity beans Person and Address both of them with the key being generated by two different sequences: person_seq and address_seq. Person has an foreign key that references the field "id" of the Address table.
In the Person ejb I have:



In the Address ejb I have:



Both tables are empty and I'm trying to add an register in the table Person and Address but I'm getting the following error:

ORA-02291: integrity constraint (ADM.FK_ADDRESS) violated - parent key not found

I'm getting some warnings when I start JBOSS:

"14:32:28,570 WARN [Ejb3Configuration] Persistence provider caller does not implements the EJB3 spec correctly. PersistenceUnitInfo.getNewTempClassLoader() is null."

"14:32:28,898 WARN [AnnotationBinder] Hibernate does not support SequenceGenerator.initialValue()"

Do you think these warns have to do with the error message?

Thank you.

Nei
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

@JoinColumn(name="addressId", referencedColumnName="id")

I don't think addressId and id are the correct values to do for your join. What is the actual column name, and if it is not address_id, then you will need an @Column for the id attribute of the Address object, and probably the same for the Person object.

If you have an attribute called "id" and no @Column, Hibernate will assume the primary key field is the name of the class plus an underscore plus "id" so for a Person object with an "id" attribute marked at the @Id, then the primary field name in the database should be "person_id"

Mark
Nei San
Greenhorn

Joined: May 08, 2007
Posts: 12
Hi,

I added the Column(name =�id�) annotation before the getId() method of Person and Address ejb but I�m getting the same error message about the foreign key violation. My Oracle tables have the following fields:

* Person

Name Type Nullable Default
-------- ------------- -------- -------
ID NUMBER
NAME VARCHAR2(80) Y
ADDRESSID NUMBER(10) Y
EMAIL VARCHAR2(80) Y
PASSWORD VARCHAR2(64) Y
GENDER VARCHAR2(20) Y NULL
...and some more fields

The field ID is the primary key and ADDRESSID is a foreign key.

* Address

Name Type Nullable Default
-------- ------------- -------- -------
ID NUMBER(10)
LINE1 VARCHAR2(255) Y NULL
COUNTRY VARCHAR2(150) Y NULL
POSTCODE VARCHAR2(50) Y NULL

The field ID is the primary key

I have two entity beans called Person and Address.
The data of the tables is entered using the register.jsp page that uses the Register.java class to save the contents to the tables as showed below:



* Register.java:





The EntityFacadeBean uses a EntityManager to save the records in the table and the annotations in the beans are:

* Person.java



* Address.java



The entity manager is responsible for recording the informations in the database, then shouldn�t it be recording the registers in the correct order: the Address first and the Person data after that?

Thank you.

Nei
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

I don't see where you add an Address record. So basically what I see is you create a Person object, with no Address object in it, and you go to insert the person into the database, but there is no corresponding address record. Since addressid is a FK it must have a value. In your model Address is the parent table of person, so there would have to be an Address record inserted before you can insert a Person record. (which normally it is the other way around)

Mark
Nei San
Greenhorn

Joined: May 08, 2007
Posts: 12
Hi,

I have a JSP page that receives the data entered by the user. This page uses JSF.

* register.jsp:



The JSF configuration is:

* faces-config.xml:



So when the user enters the data in the JSP/JSF page and press submit the class Register.java is called with the Person ejb data filled, is that correct? As the Person ejb has the methods getAddress and setAddress, shouldn�t the entity manager create the records in the table Address and in the table Person?

Thank you again.

Nei
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

OK, so if set a break point just before you call persist or save method, does the Person object have an Address object, or actually, in your case the Address object have a Person?

Mark
Nei San
Greenhorn

Joined: May 08, 2007
Posts: 12
Hi,

Sorry the late reply but I was busy with other activities. I just discovered why my code wasn't working. I had created an Oracle before insert trigger for Person and Address table that used the sequences to insert the primary key value in the tables. I didn't realize that JBOSS invokes the sequences to generate the primary key itself.

Thank you very much for your help.

Nei
 
 
subject: Using Oracle sequence with JBOSS