Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating an JPA in eclipse

 
Vallaru smitha
Ranch Hand
Posts: 87
Firefox Browser Hibernate Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at which line, you are getting the compile error.
 
Vallaru smitha
Ranch Hand
Posts: 87
Firefox Browser Hibernate Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
Firefox Browser Hibernate Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
Firefox Browser Hibernate Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
Firefox Browser Hibernate Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

code.jpg
[Thumbnail for code.jpg]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vallaru smitha
Ranch Hand
Posts: 87
Firefox Browser Hibernate Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic