random-jpa icon indicating copy to clipboard operation
random-jpa copied to clipboard

can't auto detect relationship

Open xqdd opened this issue 6 years ago • 3 comments

BaseEntity

package com.xqdd.hobby.entity;

import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;

@MappedSuperclass
@Data
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Long id;

    @CreatedDate
    protected long createTime;

    @LastModifiedDate
    private long updateTime;
}

User

package com.xqdd.hobby.entity.user;

import com.xqdd.hobby.entity.BaseEntity;
import lombok.Data;

import javax.persistence.Entity;

@Entity
@Data
public class User extends BaseEntity {

    private String wxOpenId;
    private String googleId;

}

Resource

package com.xqdd.hobby.entity.other;

import com.xqdd.hobby.entity.BaseEntity;
import com.xqdd.hobby.entity.user.User;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;

@Data
@Entity
public class Resource extends BaseEntity {
    @Column
    private String name;
    private String url;
    @ManyToOne
    private User uploader;
}

unit test

package com.xqdd.hobby;

import com.github.kuros.random.jpa.Database;
import com.github.kuros.random.jpa.JPAContext;
import com.github.kuros.random.jpa.JPAContextFactory;
import com.github.kuros.random.jpa.types.Entity;
import com.xqdd.hobby.entity.other.Resource;
import com.xqdd.hobby.entity.user.User;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

import javax.persistence.EntityManager;

@SpringBootTest
public class MarkdownTest extends AbstractTransactionalJUnit4SpringContextTests {

    @Autowired
    private EntityManager entitymanager;

    @Test
    public void test() {
        final JPAContext jpaContext = JPAContextFactory.newInstance(Database.MY_SQL, entitymanager).generate();
        var resourceEntity = Entity.of(Resource.class).createAfter(User.class);
        var resultMap = jpaContext.createAndPersist(resourceEntity);
    }
}

errors


com.github.kuros.random.jpa.exception.RandomJPAException: unable to save: class com.xqdd.hobby.entity.other.Resource

	at com.github.kuros.random.jpa.persistor.functions.FunctionProcessor.findOrSave(FunctionProcessor.java:49)
	at com.github.kuros.random.jpa.persistor.EntityPersistorImpl.persist(EntityPersistorImpl.java:98)
	at com.github.kuros.random.jpa.persistor.EntityPersistorImpl.persist(EntityPersistorImpl.java:84)
	at com.github.kuros.random.jpa.persistor.EntityPersistorImpl.persist(EntityPersistorImpl.java:106)
	at com.github.kuros.random.jpa.persistor.EntityPersistorImpl.persist(EntityPersistorImpl.java:84)
	at com.github.kuros.random.jpa.persistor.EntityPersistorImpl.persist(EntityPersistorImpl.java:68)
	at com.github.kuros.random.jpa.JPAContextImpl.persist(JPAContextImpl.java:76)
	at com.github.kuros.random.jpa.JPAContextImpl.createAndPersist(JPAContextImpl.java:119)
	at com.github.kuros.random.jpa.JPAContextImpl.createAndPersist(JPAContextImpl.java:85)
	at com.xqdd.hobby.MarkdownTest.test(MarkdownTest.java:28)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
	at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.xqdd.hobby.entity.other.Resource.uploader -> com.xqdd.hobby.entity.user.User
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:151)
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
	at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1459)
	at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1439)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:308)
	at com.sun.proxy.$Proxy113.flush(Unknown Source)
	at com.github.kuros.random.jpa.persistor.functions.PersistFunction.apply(PersistFunction.java:37)
	at com.github.kuros.random.jpa.persistor.functions.FunctionProcessor.findOrSave(FunctionProcessor.java:42)
	... 39 more
Caused by: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.xqdd.hobby.entity.other.Resource.uploader -> com.xqdd.hobby.entity.user.User
	at org.hibernate.engine.spi.CascadingActions$8.noCascade(CascadingActions.java:380)
	at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
	at org.hibernate.event.internal.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:159)
	at org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:150)
	at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:83)
	at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:38)
	at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1453)
	... 48 more

xqdd avatar Apr 29 '19 09:04 xqdd

The reason is that User need persist before Resource, I tried use withPreconditions(Before.of()) ,.createAfter() and .createBefore(), but it doesn't work

xqdd avatar Apr 29 '19 09:04 xqdd

Fix by

var user = jpaContext.createAndPersist(Entity.of(User.class)).get(User.class);
var resourceEntity = Entity.of(Resource.class).with(Resource_.uploader,user);

but need manual operation

xqdd avatar Apr 29 '19 11:04 xqdd

Currently Random-JPA, only supports Basic column types, so if there is any relational mapping, it ignores them and try to insert entity values. Hence the @ManyToOne mapping was not working.

I will take it up as an enhancement for future release.

kuros avatar May 02 '19 16:05 kuros