小编典典

在JPA中使用连接表时,我的post api中出现问题

spring-boot

我正在尝试创建一个Post API,它将一个用户添加到我的数据库中。我有3个表:1:ol__user 2:ol__roles
3:ol__users_roles ol__user和ol__roles之间没有关系,但是ol__users_roles包含两个表的ID。

资料模型

我使用了@jointable注释,我的代码如下所示:

@Entity

    @Table(name = "ol__user")
    @DynamicUpdate
    @EntityListeners(AuditingEntityListener.class)
    public class ol__user {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO,generator = "system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    @Column(name = "id", unique = true, nullable = false)
    private String user_Id;

        @Column(name = "username", nullable = true)
        private String username;
        @Column(name = "password", nullable = false)
        private String password;
        @Column(name = "salt", nullable = true)
        private String salt;
        @Column(name = "is_active", nullable = false)
        private Boolean is_active;
        @Column(name = "create_at", nullable = true)
        private Timestamp create_at;
        @Column(name = "email", nullable = false)
        private String email;
        @Column(name = "password_requested_at", nullable = true)
        private Timestamp password_requested_at;
        @Column(name = "confirmation_token", nullable = true)
        private String confirmation_token;
        @Column(name = "deleted_at", nullable = true)
        private Timestamp deleted_at;
        @Column(name = "last_login_at", nullable = true)
        private Timestamp last_login_at;
        @Column(name = "dtype", nullable = false)
        private String dtype;
        @Column(name = "temporary_password_set_at", nullable = true)
        private Timestamp temporary_password_set_at;
        @Column(name = "action_token", nullable = true)
        private String action_token;
        @Column(name = "referral_customer_email", nullable = true)
        private String referral_customer_email;
        @Column(name = "newsletter_used_flag", nullable = true)
        private Boolean newsletter_used_flag;
        @Column(name = "phone", nullable = true)
        private String phone;
        @Column(name = "status_type", nullable = true)
        private String status_type;
        @Column(name = "status_state", nullable = true)
        private String status_state;
        @Column(name = "first_name", nullable = true)
        private String first_name;
        @Column(name = "last_name", nullable = true)
        private String last_name;
        @Column(name = "external", nullable = true)
        private Boolean external;
        @Column(name = "api_key", nullable = true)
        private String api_key;
        @Column(name = "allow_point_transfer", nullable = true)
        private Boolean allow_point_transfer;




    @OneToOne(cascade = CascadeType.ALL )
    @JoinTable(name="ol__users_roles",
    joinColumns =
            {@JoinColumn(name="user_id",referencedColumnName = "id")},
    inverseJoinColumns =
            {@JoinColumn(name="role_id",referencedColumnName = "id")}

          )

    private ol__roles roles;

    public ol__roles getRoles() {
        return roles;
    }
    public void setRoles(ol__roles roles) {
        this.roles = roles;
    }
setter AND getters..

这是我的第二个表代码:

@Entity
@Table(name="ol__roles")
@EntityListeners(AuditingEntityListener.class)
public class ol__roles {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", unique = true, nullable = false)
   private Integer roleId;
    @Column(name="name",nullable = true)
    private String name;
    @Column(name="role",nullable = true)
    private String role;
    @Column(name="is_master",nullable = true)
    private Boolean is_master;

setter and getters ...

这是我的第三张表代码:

@Entity
@Table(name="ol__users_roles")
@EntityListeners(AuditingEntityListener.class)
public class ol__users_roles {
    @Id
    @Column(name = "user_id",unique = true,nullable = false)
    private String user_id;


    @Column(name = "role_id",unique = true,nullable = false)
    private Integer role_id;

setter and getters ...

ol__users_roles没有唯一的ID,仅具有这2个外键。

在我的控制器中,我有以下代码:

 @PostMapping("/admin/data")
    public ol__user createUserAdmin(@Valid @RequestBody ol__user ol__user)
    {


        return userRepository.save(ol__user);
    }

每当我调用此API时:

{
    "username": "adminssroos",
    "firstName": "adminsss",
    "lastName": "adminssnew",
    "email": "nejatianadmhhhs@admin.com",
    "phone": "09354006090",
    "salt": "123455",
    "roles": {
        "role_id": "13"

    },
    "password": "123@123@Mm",
    "is_active": true,
    "create_at": "2020-03-01",
    "dtype": "admin"
}

我收到以下错误:

> 2020-04-07 02:27:48.628  INFO 13242 --- [nio-8080-exec-1]
> o.s.web.servlet.DispatcherServlet        : Completed initialization in
> 10 ms Hibernate: 
>     select
>         nextval ('hibernate_sequence') Hibernate: 
>     insert 
>     into
>         ol__roles
>         (is_master, name, role, id) 
>     values
>         (?, ?, ?, ?) 2020-04-07 02:27:49.157  WARN 13242 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL
> Error: 0, SQLState: 23502 2020-04-07 02:27:49.157 ERROR 13242 ---
> [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR:
> null value in column "role" violates not-null constraint   Detail:
> Failing row contains (18, null, null, null). 2020-04-07 02:27:49.175
> ERROR 13242 --- [nio-8080-exec-1] o.h.i.ExceptionMapperStandardImpl   
> : HHH000346: Error during managed flush
> [org.hibernate.exception.ConstraintViolationException: could not
> execute statement] 2020-04-07 02:27:49.242 ERROR 13242 ---
> [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    :
> Servlet.service() for servlet [dispatcherServlet] in context with path
> [] threw exception [Request processing failed; nested exception is
> org.springframework.dao.DataIntegrityViolationException: could not
> execute statement; SQL [n/a]; constraint [role]; nested exception is
> org.hibernate.exception.ConstraintViolationException: could not
> execute statement] with root cause
> 
> org.postgresql.util.PSQLException: ERROR: null value in column "role"
> violates not-null constraint   Detail: Failing row contains (18, null,
> null, null).  at
> org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
> ~[postgresql-42.2.5.jar:42.2.5]   at
> org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183)
> ~[postgresql-42.2.5.jar:42.2.5]   at
> org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:308)
> ~[postgresql-42.2.5.jar:42.2.5]   at
> org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
> ~[postgresql-42.2.5.jar:42.2.5]   at
> org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
> ~[postgresql-42.2.5.jar:42.2.5]   at
> org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:143)
> ~[postgresql-42.2.5.jar:42.2.5]   at
> org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:120)
> ~[postgresql-42.2.5.jar:42.2.5]   at
> com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61)
> ~[HikariCP-3.2.0.jar:na]  at
> com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java)
> ~[HikariCP-3.2.0.jar:na]  at
> org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3176)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3690)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:90)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:356)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1454)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:511)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3290)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2486)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:473)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:178)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl.java:39)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:271)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:104)
> ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]   at
> org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:532)
> ~[spring-orm-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:746)
> ~[spring-tx-5.1.8.RELEASE.jar:5.1.8.RELEASE]  at
> org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:714)
> ~[spring-tx-5.1.8.RELEASE.jar:5.1.8.RELEASE]  at
> org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:534)
> ~[spring-tx-5.1.8.RELEASE.jar:5.1.8.RELEASE]  at
> org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:305)
> ~[spring-tx-5.1.8.RELEASE.jar:5.1.8.RELEASE]  at
> org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
> ~[spring-tx-5.1.8.RELEASE.jar:5.1.8.RELEASE]  at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
> ~[spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
> ~[spring-tx-5.1.8.RELEASE.jar:5.1.8.RELEASE]  at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
> ~[spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:138)
> ~[spring-data-jpa-2.1.9.RELEASE.jar:2.1.9.RELEASE]    at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
> ~[spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
> ~[spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
> ~[spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
> ~[spring-data-commons-2.1.9.RELEASE.jar:2.1.9.RELEASE]    at
> org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
> ~[spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
> ~[spring-aop-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> com.sun.proxy.$Proxy115.save(Unknown Source) ~[na:na]     at
> com.Loyalty.OpenLoyalty.Controllers.Ol__userController.createUserAdmin(Ol__userController.java:41)
> ~[classes/:na]    at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method) ~[na:na]  at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> ~[na:na]  at
> java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> ~[na:na]  at
> java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
>   at
> org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
> ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
> ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
> ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]  at
> org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
> ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]  at
> org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
> ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]  at
> org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
> ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]  at
> org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
> ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]  at
> org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
> ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]  at
> org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
> ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]  at
> org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
> ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]  at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
> ~[spring-webmvc-5.1.8.RELEASE.jar:5.1.8.RELEASE]  at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
> ~[tomcat-embed-websocket-9.0.21.jar:9.0.21]   at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
> ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
> ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
> ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
> ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
> ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
> ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
> ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
> ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]     at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
> ~[na:na]  at
> java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
> ~[na:na]  at
> org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
> ~[tomcat-embed-core-9.0.21.jar:9.0.21]    at
> java.base/java.lang.Thread.run(Thread.java:830) ~[na:na]

我不知道为什么我的API插入ol__roles而不是ol__users_roles,有人可以帮助我吗?


阅读 219

收藏
2020-05-30

共1个答案

小编典典

我解决了你的问题。看一下这个:

ol__roles 模型:

@Entity
@Table(name="ol__roles")
@EntityListeners(AuditingEntityListener.class)
public class ol__roles {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", unique = true, nullable = false)
   private Integer id;

    @Column(name="name",nullable = true)
    private String name;

    @Column(name="role",nullable = true)
    private String role;

    @Column(name="is_master",nullable = true)
    private Boolean is_master;

    @JsonIgnore
    @ManyToMany(mappedBy="roles",cascade = {CascadeType.MERGE},fetch= FetchType.EAGER)
    private Set<ol__user> users = new HashSet<ol__user>();
//getter and setters
}

ol__user 模型:

@Entity

    @Table(name = "ol__user")
    @DynamicUpdate
    @EntityListeners(AuditingEntityListener.class)
    public class ol__user {

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO, generator = "system-uuid")
        @GenericGenerator(name = "system-uuid", strategy = "uuid")
        @Column(name = "id", unique = true, nullable = false)
        private String id;

        @Column(name = "username", nullable = true)
        private String username;

        @Column(name = "password", nullable = false)
        private String password;

        @Column(name = "salt", nullable = true)
        private String salt;

        @Column(name = "is_active", nullable = false)
        private Boolean is_active;

        @Column(name = "create_at", nullable = true)
        private Timestamp create_at;

        @Column(name = "email", nullable = false)
        private String email;

        @Column(name = "password_requested_at", nullable = true)
        private Timestamp password_requested_at;

        @Column(name = "confirmation_token", nullable = true)
        private String confirmation_token;

        @Column(name = "deleted_at", nullable = true)
        private Timestamp deleted_at;

        @Column(name = "last_login_at", nullable = true)
        private Timestamp last_login_at;

        @Column(name = "dtype", nullable = false)
        private String dtype;

        @Column(name = "temporary_password_set_at", nullable = true)
        private Timestamp temporary_password_set_at;

        @Column(name = "action_token", nullable = true)
        private String action_token;

        @Column(name = "referral_customer_email", nullable = true)
        private String referral_customer_email;

        @Column(name = "newsletter_used_flag", nullable = true)
        private Boolean newsletter_used_flag;

        @Column(name = "phone", nullable = true)
        private String phone;

        @Column(name = "status_type", nullable = true)
        private String status_type;

        @Column(name = "status_state", nullable = true)
        private String status_state;

        @Column(name = "first_name", nullable = true)
        private String first_name;

        @Column(name = "last_name", nullable = true)
        private String last_name;

        @Column(name = "external", nullable = true)
        private Boolean external;

        @Column(name = "api_key", nullable = true)
        private String api_key;

        @Column(name = "allow_point_transfer", nullable = true)
        private Boolean allow_point_transfer;

        @ManyToMany(cascade = {CascadeType.MERGE})
        @JoinTable(name="ol__users_roles",
                joinColumns={@JoinColumn(name="user_id")},
                inverseJoinColumns={@JoinColumn(name="role_id")})
        private Set<ol__roles> roles = new HashSet<ol__roles>();
//getters and setters
}

就是这样!您无需为ol__users_roles定义任何模型。希望这对您有帮助!

2020-05-30