Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » get sequence number through Eclipselink
get sequence number through Eclipselink [message #998157] Wed, 09 January 2013 13:09 Go to next message
Jorg Heymans is currently offline Jorg Heymans
Messages: 26
Registered: July 2009
Location: Belgium
Junior Member
Hi,

I have read the wiki so i know that for getting a sequence value i should be able to do this

long id = em.unwrap(Session.class).getNextSequenceNumberValue(Employee.class).longValue();


However when i try this i get a NPE

Caused by: java.lang.NullPointerException: null
        at org.eclipse.persistence.internal.sequencing.SequencingManager.getNextValue(SequencingManager.java:1067) ~[eclipselink-2.4.1.jar:2.4.1.v20121003-ad44345]
        at org.eclipse.persistence.internal.sequencing.SequencingManager.getNextValue(SequencingManager.java:266) ~[eclipselink-2.4.1.jar:2.4.1.v20121003-ad44345]
        at org.eclipse.persistence.internal.sessions.AbstractSession.getNextSequenceNumberValue(AbstractSession.java:2457) ~[eclipselink-2.4.1.jar:2.4.1.v20121003-ad44345]



Our sequence is just defined like this

@SequenceGenerator(name = "AppId", sequenceName = "SEQ_APP", allocationSize = 500)


Reason i'm asking is that we have mixed jpa en jdbc inserts and i would like them to pull numbers from the same sequence number pool, otherwise the jdbc inserts will have ids that are 500 apart each time which is not desired.

Any thoughts ?
Jorg
Re: get sequence number through Eclipselink [message #998362 is a reply to message #998157] Wed, 09 January 2013 21:34 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1023
Registered: July 2009
Senior Member
Are you able to insert a new Employee instance using JPA and have the sequence generated? For example em.persist(new Employee());

If not, then I believe you might be missing the @GeneratedValue annotation on the ID that ties the defined SequenceGenerator to the Employee class, and might result in the NPE you see when trying to get it through native API.

Best Regards,
Chris
Re: get sequence number through Eclipselink [message #998506 is a reply to message #998157] Thu, 10 January 2013 06:06 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom Eugelink
Messages: 807
Registered: July 2009
Senior Member
On 2013-01-09 14:09, Jorg Heymans wrote:
> Reason i'm asking is that we have mixed jpa en jdbc inserts and i would like them to pull numbers from the same sequence number pool, otherwise the jdbc inserts will have ids that are 500 apart each time which is not desired.

Not sure why your call is failing, but I do about the same and that works great. So this should work.

Tom
Re: get sequence number through Eclipselink [message #998559 is a reply to message #998506] Thu, 10 January 2013 08:35 Go to previous messageGo to next message
Jorg Heymans is currently offline Jorg Heymans
Messages: 26
Registered: July 2009
Location: Belgium
Junior Member
Yes @GeneratedValue is not present on the @Id because it works fine without. This is our setup:

- the @SequenceGenerator is defined on a @MappedSuperclass that all our entities extend from. So we use one sequence for all entities.
@MappedSuperclass
@SequenceGenerator(name = "AppIdentity", sequenceName = "SEQ_APP", allocationSize = 500)
public abstract class AbstractDatabaseEntity {
}

- each entity extends this mapped superclass and defines its @Id with something like this:
    @Id
    @Column(name = "ID")
    private String id;


Apparently there is no need for @GeneratedValue on each @Id definition, it defaults to the defined sequencegenerator in the base class.

So when i then explicitly add an @GeneratedValue annotation to the entity
    @Id
    @Column(name = "ID")
    @GeneratedValue(generator="AppIdentity")
    private String id;


then the NPE disappears. Is this a bug then ? I can add it everywhere no problem, but perhaps EL should take into account the mappedsuperclass when searching for the sequencegenerator.

Thanks!



Re: get sequence number through Eclipselink [message #998709 is a reply to message #998559] Thu, 10 January 2013 14:39 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

If you don't use @GeneratedValue, then tere is no generated value, the id will be null, and you cannot get the sequence generator for the class, as it has none.

To generate an id you need to use @GeneratedValue.


James : Wiki : Book : Blog : Twitter
Re: get sequence number through Eclipselink [message #1004062 is a reply to message #998157] Tue, 22 January 2013 14:09 Go to previous messageGo to next message
GianMaria Romanato is currently offline GianMaria Romanato
Messages: 61
Registered: July 2009
Member
Hello, I have another question on the same subject.

EclipseLink does support custom sequence generators, as explained here:
http://wiki.eclipse.org/EclipseLink/Examples/JPA/CustomSequencing#Using_the_Sequence

When using a sequence generator that returns a sequence value that is not a number (for example an UUID, or any other string), the above method is not applicable because it forces the returned sequence value to be a subtype of Number.

I found an alternative path:
entityManager.unwrap(org.eclipse.persistence.jpa.JpaEntityManager.class).getServerSession().getSequencing().getNextValue(domainClass);


and it seems to work properly, however it is using the org.eclipse.persistence.internal.sequencing.Sequencing interface which belongs to an internal package. Is there a safer way of obtaining the next sequence value when a custom generator is used and the data type is not a number?

Thanks in advance
Giamma.




Developing for Virgo using Eclipse PDE: http://bit.ly/1w0tTit
Global JNDI support in Virgo: http://bit.ly/1to42mn
How to use Hyperic to monitor Virgo Server: http://bit.ly/W1Fst9
Re: get sequence number through Eclipselink [message #1004104 is a reply to message #1004062] Tue, 22 January 2013 15:27 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Please log a bug to have the API return Object (probably a new API, getNextSequenceValue()).

Your workaround is fine.


James : Wiki : Book : Blog : Twitter
Re: get sequence number through Eclipselink [message #1004119 is a reply to message #1004104] Tue, 22 January 2013 16:04 Go to previous message
GianMaria Romanato is currently offline GianMaria Romanato
Messages: 61
Registered: July 2009
Member
Done, 398764

Thanks
Giamma.


Developing for Virgo using Eclipse PDE: http://bit.ly/1w0tTit
Global JNDI support in Virgo: http://bit.ly/1to42mn
How to use Hyperic to monitor Virgo Server: http://bit.ly/W1Fst9
Previous Topic:tenant property not found on cascading delete
Next Topic:Eclipselink and oderby does not work on criteria fetch query
Goto Forum:
  


Current Time: Sun Sep 21 02:12:07 GMT 2014

Powered by FUDForum. Page generated in 0.02342 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software