Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA&x2B;MySQL—数据库生成的列值_Java_Mysql_Spring Boot_Jpa - Fatal编程技术网

Java JPA&x2B;MySQL—数据库生成的列值

Java JPA&x2B;MySQL—数据库生成的列值,java,mysql,spring-boot,jpa,Java,Mysql,Spring Boot,Jpa,我使用的列总是生成为公式。当我尝试使用JPA保存实体值时,我得到 java.sql.SQLException: The value specified for generated column 'bucket' in table 'discover_cache' is not allowed. 类别定义: @Entity @Getter @NoArgsConstructor @AllArgsConstructor @Table(name = "discover_cache") @IdClass

我使用的列总是生成为公式。当我尝试使用JPA保存实体值时,我得到

java.sql.SQLException: The value specified for generated column 'bucket' in table 'discover_cache' is not allowed.
类别定义:

@Entity
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "discover_cache")
@IdClass(DiscoverCacheId.class)
public class DiscoverCache {

    @Id
    @Column(length = 36)
    private String userId;

    @Id
    private int postId;

    @Column(columnDefinition = "tinyint(4) GENERATED ALWAYS AS (floor(TO_SECONDS(`created_date`) / 900) % 17) STORED NOT NULL")
    @Id
    private short bucket;

    @CreatedDate
    @Column(name = "created_date", updatable = false, columnDefinition = "DATETIME(3)")
    @JsonIgnore
    private Instant createdDate = Instant.now();

    public DiscoverCache(String userId, int postId) {
        this.userId = userId;
        this.postId = postId;
    }
}

@EqualsAndHashCode
@NoArgsConstructor
public class DiscoverCacheId implements Serializable {

    @Column(length = 36)
    private String userId;

    private int postId;

    @Column(columnDefinition = "tinyint(4) GENERATED ALWAYS AS (floor(TO_SECONDS(`created_date`) / 900) % 17) STORED NOT NULL")
    private short bucket;

    public DiscoverCacheId(String userId, int postId, short bucket) {
        this.userId = userId;
        this.postId = postId;
        this.bucket = bucket;
    }
}

我如何告诉JPA忽略persist上的这个值,以便数据库计算我的值?我当然可以使用本机查询来保存它,但我想使用JpaRepository将insertable和updateable设置为false

@Column(name="user_id", nullable = true, insertable = false, updatable = false)
var userId: Long? = null

将类型更改为
Short
,而不是
Short
,并且(如果您使用HIbernate作为JPA提供程序)使用
@DynamicInsert
和/或
@DynamicUpdate
注释您的实体,它们从insert/update语句中排除空值。