Skip to main content



      Home
Home » Eclipse Projects » EclipseLink » How to obtain sequence value?
How to obtain sequence value? [message #1085727] Tue, 13 August 2013 05:23 Go to next message
Eclipse UserFriend
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 09:52 Go to previous messageGo to next message
Eclipse UserFriend
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).
Re: How to obtain sequence value? [message #1085938 is a reply to message #1085888] Tue, 13 August 2013 11:09 Go to previous messageGo to next message
Eclipse UserFriend
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 11:24 Go to previous message
Eclipse UserFriend
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
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: Thu Jul 24 20:28:44 EDT 2025

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

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

Back to the top