Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » How to obtain sequence value?
How to obtain sequence value? [message #1085727] Tue, 13 August 2013 09:23 Go to next message
Shamil Valiev is currently offline Shamil ValievFriend
Messages: 2
Registered: August 2013
Junior Member
Hello there.
I have a project on EclipseLink + Weld + RestEasy all work in JBoss AS7. I use JTA and I don't know how to properly obtain a sequence value to return it to client. I have an AppSession entity object with a primary key id annotated with @TableGenerator. When I persist that object the Id is 0, although it has a value greater than 0 in MySQL table. I would prefer to generate a sequence value and keep it in a local variable, set it to the id field and then return it to client. Is it possible? What is the best practice to work with sequence values with JTA transactions?
Re: How to obtain sequence value? [message #1085888 is a reply to message #1085727] Tue, 13 August 2013 13:52 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

If you are using TABLE id generation, then after you call persist() on the object, its id will be assigned. If you are using IDENTITY id generation, then you must call flush() to assign the id (or use TABLE id generation).


James : Wiki : Book : Blog : Twitter
Re: How to obtain sequence value? [message #1085938 is a reply to message #1085888] Tue, 13 August 2013 15:09 Go to previous messageGo to next message
Shamil Valiev is currently offline Shamil ValievFriend
Messages: 2
Registered: August 2013
Junior Member
James Sutherland wrote on Tue, 13 August 2013 09:52
If you are using TABLE id generation, then after you call persist() on the object, its id will be assigned. If you are using IDENTITY id generation, then you must call flush() to assign the id (or use TABLE id generation).


I'm using TABLE id generation and Id is always 0. If I flush and refresh then Id is correct.

        @Transactional
	public int createSession(int userId, Date date) {

		Query q = em.createQuery("update CoreSession t "
				+ "set t.sessionCloseTime = :sessionCloseTime, "
				+ "    t.isActive = 0 " + "where t.isActive = 1 "
				+ "  and t.userId = :userId");
		q.setParameter("sessionCloseTime", date);
		q.setParameter("userId", userId);
		q.executeUpdate();

		CoreSession coreSession = new CoreSession();
		try {
			coreSession.setUserId(userId);
			coreSession.setIsActive(1);
			coreSession.setSessionStartTime(date);
			coreSession.setLastRequestTime(date);
			em.persist(coreSession);
			//em.flush();
			//em.refresh(coreSession);

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		return coreSession.getId();
	}


Is there possibility in EclipseLink to retrieve next sequence value before persist/merge/etc?
Re: How to obtain sequence value? [message #1096567 is a reply to message #1085938] Wed, 28 August 2013 15:24 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Please include your code, are you using the @GeneratedValue annotation? If your object has no id, then I can't see how refresh could work?

See,
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing


James : Wiki : Book : Blog : Twitter
Previous Topic:Criteria API joinList inheritance Problem
Next Topic:Moxy is not honoring "type" attribute in xml-element tag in EclipseLink 2.4.2
Goto Forum:
  


Current Time: Mon Dec 22 05:07:28 GMT 2014

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

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