mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#310 - Child bean with @Id long id = 0 ... when cascade saved from parent ... tries to update instead of insert resulting in OptimisticLockingException
This commit is contained in:
+7
-3
@@ -35,13 +35,13 @@ public class TestBeanDescriptorHasIdProperty extends BaseTestCase {
|
||||
|
||||
Order order = new Order();
|
||||
|
||||
Assert.assertFalse(beanDescriptor.hasIdProperty(getIntercept(order)));
|
||||
Assert.assertFalse(beanDescriptor.hasIdValue(entityBean(order)));
|
||||
Assert.assertFalse(beanDescriptor.hasVersionProperty(getIntercept(order)));
|
||||
|
||||
order.setId(23);
|
||||
order.setUpdtime(new Timestamp(System.currentTimeMillis()));
|
||||
|
||||
Assert.assertTrue(beanDescriptor.hasIdProperty(getIntercept(order)));
|
||||
Assert.assertTrue(beanDescriptor.hasIdValue(entityBean(order)));
|
||||
Assert.assertTrue(beanDescriptor.hasVersionProperty(getIntercept(order)));
|
||||
|
||||
}
|
||||
@@ -61,7 +61,11 @@ public class TestBeanDescriptorHasIdProperty extends BaseTestCase {
|
||||
order.setName("custName");
|
||||
Assert.assertFalse(beanDescriptor.hasIdPropertyOnly(ebi));
|
||||
}
|
||||
|
||||
|
||||
private EntityBean entityBean(Object bean) {
|
||||
return (EntityBean)bean;
|
||||
}
|
||||
|
||||
private EntityBeanIntercept getIntercept(Object bean) {
|
||||
return ((EntityBean)bean)._ebean_getIntercept();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.avaje.tests.model.zero;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class TestWithZero extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testInsertUpdate() {
|
||||
|
||||
|
||||
WithZero withZero = new WithZero();
|
||||
assertEquals(0, withZero.getId());
|
||||
assertEquals(0, withZero.getVersion());
|
||||
assertNull(withZero.getName());
|
||||
|
||||
WithZeroParent parent = new WithZeroParent();
|
||||
parent.getChildren().add(withZero);
|
||||
|
||||
Ebean.save(parent);
|
||||
|
||||
parent.getChildren().add(new WithZero());
|
||||
|
||||
//withZero.setName("Foo");
|
||||
Ebean.save(parent);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.avaje.tests.model.zero;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
public class WithZero {
|
||||
|
||||
@Id
|
||||
long id = 0;
|
||||
|
||||
String name = null;
|
||||
|
||||
@ManyToOne
|
||||
WithZeroParent parent = null;
|
||||
|
||||
@Version
|
||||
long version = 0;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public WithZeroParent getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(WithZeroParent parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.avaje.tests.model.zero;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "parent")
|
||||
public class WithZeroParent {
|
||||
@Id
|
||||
@Column(name = "id")
|
||||
private int id;
|
||||
|
||||
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
|
||||
private List<WithZero> children;
|
||||
|
||||
public List<WithZero> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setChildren(final List<WithZero> children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user