mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-22 05:02:10 +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:
@@ -28,6 +28,7 @@ import com.avaje.ebeaninternal.server.deploy.id.IdBinder;
|
||||
import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanPropertyLists;
|
||||
import com.avaje.ebeaninternal.server.el.*;
|
||||
import com.avaje.ebeaninternal.server.persist.DmlUtil;
|
||||
import com.avaje.ebeaninternal.server.query.CQueryPlan;
|
||||
import com.avaje.ebeaninternal.server.query.CQueryPlanStats.Snapshot;
|
||||
import com.avaje.ebeaninternal.server.query.SplitName;
|
||||
@@ -1714,7 +1715,7 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
|
||||
// not using Id generator so just base on isLoaded()
|
||||
return !ebi.isLoaded();
|
||||
}
|
||||
if (!hasIdProperty(ebi)) {
|
||||
if (!hasIdValue(ebi.getOwner())) {
|
||||
// No Id property means it must be an insert
|
||||
return true;
|
||||
}
|
||||
@@ -1729,9 +1730,9 @@ public class BeanDescriptor<T> implements MetaBeanInfo {
|
||||
public boolean hasIdPropertyOnly(EntityBeanIntercept ebi) {
|
||||
return ebi.hasIdOnly(idPropertyIndex);
|
||||
}
|
||||
|
||||
public boolean hasIdProperty(EntityBeanIntercept ebi) {
|
||||
return idPropertyIndex > -1 && ebi.isLoadedProperty(idPropertyIndex);
|
||||
|
||||
public boolean hasIdValue(EntityBean bean) {
|
||||
return (idProperty != null && !DmlUtil.isNullOrZero(idProperty.getValue(bean)));
|
||||
}
|
||||
|
||||
public boolean hasVersionProperty(EntityBeanIntercept ebi) {
|
||||
|
||||
+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