aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Creating an JPA in eclipse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Creating an JPA in eclipse" Watch "Creating an JPA in eclipse" New topic
Author

Creating an JPA in eclipse

Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

Hi,

I am trying to create a java persistance using eclipse, MySQL and Glassfish.

First i have created a table with Name and Password and sequece generator table ( sequece : seq and last val columns)

created an entity, below is my code
import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.TableGenerator;

/**
* Entity implementation class for Entity: User
*
*/
@Entity

public class User implements Serializable {


@Id
@TableGenerator(name="sequence",
table="sequence",
pkColumnName="name",
valueColumnName="last_val",
pkColumnValue="seq",
allocationSize=1)
@GeneratedValue(strategy=GenerationType.TABLE,generator="sequence")
private int seq;
public int getSeq() {
return seq;
}


public void setSeq(int seq) {
this.seq = seq;
}
................ continued

but i get an compile time error at " column seq cannot be resolved". Could anyone please help me.

Thanks
Smitha
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
at which line, you are getting the compile error.


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

@Id : it gives an error "column seq cannot be resolved".

I have googled few more examples and in one the example the sequence genereator is not even created. So, my doubt do this value get stored some where

@Id ----------------Getting any error "column id cannot be resolved"
@GeneratedValue
int id;

If you have any simple example just to understand concept please that would be helpful.
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi

How about a simple code something like :



I think that you forget the Column & Table annotation
how about a long like id ? is more standard.




Regards,
M


SCJP, SCJD, SCWCD, OCPJBCD
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
MySQL does not support sequences. It is supported by Oracle.

Instead of doing this step-


Do these two steps-



Now you are ready to use your table generator.
Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

thanks that worked.

But i was working on example from http://www.screaming-penguin.com/node/4441 and the table used in this example does not contain any id column . I have gone through blue prints and it says about @GeneratedValue and identity. So, i tried to use it but that too failed.

If you get a chance could you please look ...

thanks once again


Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

Thanks Amandeep.

But i have created a table not sequence generator. One is user table and other is Sequence table (which is used for sequence generation+

Do these two tables should have once field in common or the sequence value directly gets stored in sequence table.

sorry I am new to the concept and trying understand with a simple example.

Your help is really apperciated.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
The initialValue element is pretty self-explanatory. AllocationSize
specifies by how much the sequence is incremented each time a value is generated

The default values for initialValue and allocationSize are 0 and 50, respectively.

Do these two tables should have once field in common or the sequence value directly gets stored in sequence table.


Well, these both table's have no field common. The sequence value gets directly stored in your users table.

Taking your example- (@Id was missing in your code.)



Here it means Column name seq, will have unique value, everytime we do insert in users table table.
As in your case initialValue attribute value 0 will be defaulted. And everytime , you insert value it will be incremented by 1 as per allocation size.
Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

Sorry, still its not clear to me

please find my code as attachment along with the tables

As per your pervious response Id was missing , I am not sure which Id you are mentioning.

Thanks
Smitha.



[Thumbnail for code.jpg]

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

In your entity, you have a field called "seq", but your user table doesn't have any column called "seq". You must make one in your user table. Also, as Amandeep said, this should be declared as being the primary key of the user table, so @Id must be set to the field.

The primary key, identified by "seq", will be retrieved from your "sequence" table, and set into the "seq" field of the user entity.


[My Blog]
All roads lead to JavaRanch
Vallaru smitha
Ranch Hand

Joined: Aug 19, 2008
Posts: 87

Thanks guys.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Creating an JPA in eclipse