-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: @Version not working
PostPosted: Fri Dec 08, 2006 2:03 pm 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hi,
I have a simple application which is trying to persist a simple Pojo.
I wish to use optimistic locking so I use the @Version tag in my Pojo.
Here is the POJO.

<PRE>

@Entity
@Table(name="TAddress")
@SequenceGenerator(name="ADDRESS_SEQ", sequenceName="SADDRESS", allocationSize=1)

public class Address {
private String city;
private String street;

@Version
private long version;

private int i = -1;

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ADDRESS_SEQ")
public int getI() {
return i;
}


public void setI(int i) {
this.i = i;
}

public long getVersion() {
return version;
}

public void setVersion(long version) {
this.version = version;
}

public String getCity() {
return city;
}

public void setCity( String city) {
this.city = city;
}

public String getStreet() {
return street;
}

public void setStreet( String street) {
this.street = street;
}
}

</PRE>

However, no matter how many times I update and persist this POJO the version attribute always stays at zero. Hibernate does not increment it for me.

Here is an example of the code where I am trying to update:
<PRE>
public void testAddressVersions() {
// one address
System.out.println(">> testAddressVersions()");
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("h-source");
EntityManager em = emf.createEntityManager();
Address address = new Address();
em.getTransaction().begin();
em.persist(address);
em.getTransaction().commit();

em.getTransaction().begin();
address.setCity("Paris2");
em.persist(address);
em.getTransaction().commit();

em.close();
emf.close();
}
</PRE>



Any help appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 2:32 pm 
Beginner
Beginner

Joined: Fri Oct 07, 2005 5:35 am
Posts: 38
Location: France
Your version annotation is on the field and your Id annotation is on a method. You can not mix annotation on field and on method.

Move your version annotation on the get method. It will work fine.

If you choose to put annotation on method, all the annotation on the field are ignored. (you can also put all your annotation on field but no mix)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 2:33 pm 
Beginner
Beginner

Joined: Fri Oct 07, 2005 5:35 am
Posts: 38
Location: France
Your version annotation is on the field and your Id annotation is on a method. You can not mix annotation on field and on method.

Move your version annotation on the get method. It will work fine.

If you choose to put annotation on method, all the annotation on the field are ignored. (you can also put all your annotation on field but no mix)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 6:44 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
That's great, thanks for that, it worked.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.