|
55 | 55 | * @author Mark Paluch
|
56 | 56 | * @author Jens Schauder
|
57 | 57 | * @author Greg Turnquist
|
| 58 | + * @author Yanming Zhou |
58 | 59 | */
|
59 | 60 | public class JpaMetamodelEntityInformation<T, ID> extends JpaEntityInformationSupport<T, ID> {
|
60 | 61 |
|
@@ -219,13 +220,38 @@ public Object getCompositeIdAttributeValue(Object id, String idAttribute) {
|
219 | 220 | @Override
|
220 | 221 | public boolean isNew(T entity) {
|
221 | 222 |
|
222 |
| - if (versionAttribute.isEmpty() |
223 |
| - || versionAttribute.map(Attribute::getJavaType).map(Class::isPrimitive).orElse(false)) { |
| 223 | + if (versionAttribute.isEmpty()) { |
224 | 224 | return super.isNew(entity);
|
225 | 225 | }
|
226 | 226 |
|
227 | 227 | BeanWrapper wrapper = new DirectFieldAccessFallbackBeanWrapper(entity);
|
228 | 228 |
|
| 229 | + if (versionAttribute.map(Attribute::getJavaType).map(Class::isPrimitive).orElse(false)) { |
| 230 | + return versionAttribute.map(it -> { |
| 231 | + Object version = wrapper.getPropertyValue(it.getName()); |
| 232 | + if (version instanceof Number value) { |
| 233 | + PersistenceProvider provider = PersistenceProvider.fromMetamodel(metamodel); |
| 234 | + if (provider == PersistenceProvider.HIBERNATE) { |
| 235 | + // Hibernate use 0 or user provided positive initial value as seed of integer version |
| 236 | + if (value.longValue() < 0) { |
| 237 | + // see org.hibernate.engine.internal.Versioning#isNullInitialVersion() |
| 238 | + return true; |
| 239 | + } |
| 240 | + // TODO Compare version to initial value (field value of transient entity) |
| 241 | + // It's unknown if equals because entity maybe transient or just persisted |
| 242 | + // But it's absolute not new if not equals |
| 243 | + } else if (provider == PersistenceProvider.ECLIPSELINK) { |
| 244 | + // EclipseLink always use 1 as seed of integer version |
| 245 | + if (value.longValue() < 1) { |
| 246 | + return true; |
| 247 | + } |
| 248 | + // It's unknown because the value may be initial value or persisted entity version |
| 249 | + } |
| 250 | + } |
| 251 | + return super.isNew(entity); |
| 252 | + }).orElseThrow(); // should never throw |
| 253 | + } |
| 254 | + |
229 | 255 | return versionAttribute.map(it -> wrapper.getPropertyValue(it.getName()) == null).orElse(true);
|
230 | 256 | }
|
231 | 257 |
|
|
0 commit comments