V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
vansl
V2EX  ›  科技

jpa 之自定义中间表

  •  
  •   vansl · Jul 22, 2018 · 3030 views
    This topic created in 2849 days ago, the information mentioned may be changed or developed.

    我发现 jpa 在自己创建一个中间表的时候,很多注解都失效了。
    比如无法自己设置一个主键,这个还好理解,使用联合主键。
    但是 @CreatedDate 这个注解也失效是为什么呢?该怎么解决呢
    附上代码:

    /* 课程的学生中间表 */
    @Table(name = "t_course_student")
    @Entity
    @IdClass(CourseStudentKey.class)
    @EntityListeners(AuditingEntityListener.class)
    public class CourseStudent {
        /* 创建时间 */
        @Column(name = "created_at")
        @CreatedDate
        private Long createdTime;
     	...
    }
    

    失效指的是无法在插入数据的时候自动带上时间了,created_at 这个字段为空。

    3 replies    2018-07-22 18:54:16 +08:00
    vansl
        1
    vansl  
    OP
       Jul 22, 2018 via iPhone
    我发现 jpa 会自动忽略自己定义的字段,那么这种自定义中间表是不是只能另写 sql 语句或者数据库函数操作了。
    wdlth
        2
    wdlth  
       Jul 22, 2018
    是不是少了 @EnableJpaAuditing
    vansl
        3
    vansl  
    OP
       Jul 22, 2018 via iPhone
    @wdlth 添加过了。其他非中间表加这个字段是没问题的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   875 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 21:38 · PVG 05:38 · LAX 14:38 · JFK 17:38
    ♥ Do have faith in what you're doing.