添加新的@Lob字段后,JPA实体未保存在数据库中

时间:2019-05-24 12:20:46

标签: java spring jpa

我有一个与未保存在数据库中的JPA实体相关的问题,所以 在jpa实体中添加新的clob字段后-实体未保存在数据库中 我使用的是Websphere,Spring 4.3.13和Oracle11g

之前我的jpa实体

import lombok.*;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.envers.Audited;

import javax.persistence.*;
import java.sql.Timestamp;

@Entity
@Audited
@Table(name = "UserRecord")
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@AllArgsConstructor
@Getter
@Setter
@Access(AccessType.FIELD)
public class UserRecord {
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @Column(name = "PR_KEY")
    private String prKey;

    //Business Key
    @Column(name = "businessId", length = 100, unique = false)
    private String businessId;

    //Business Key
    @Column(name = "name", length = 100, nullable = false)
    private String name;

    //Business Key
    @Column(name = "surname", length = 100, nullable = false)
    private String surname;

    @Column(name = "updateDateTime", length = 50, nullable = false)
    private Timestamp updateDateTime;

    @Version
    private int version;

    @Column(name = "user", length = 100000)
    @Lob
    private byte[] user;


    public UserRecord(String businessId, String name, String surname, Timestamp updateDateTime, byte[] user) {
        this.businessId = businessId;
        this.name = name;
        this.surname = surname;
        this.updateDateTime = updateDateTime;
        this.user = user;
    }
}

及之后

import lombok.*;
    import org.hibernate.annotations.GenericGenerator;
    import org.hibernate.envers.Audited;

    import javax.persistence.*;
    import java.sql.Timestamp;

    @Entity
    @Audited
    @Table(name = "UserRecord")
    @NoArgsConstructor(access = AccessLevel.PUBLIC)
    @AllArgsConstructor
    @Getter
    @Setter
    @Access(AccessType.FIELD)
    public class UserRecord {
        @Id
        @GeneratedValue(generator = "uuid")
        @GenericGenerator(name = "uuid", strategy = "uuid2")
        @Column(name = "PR_KEY")
        private String prKey;

        //Business Key
        @Column(name = "businessId", length = 100, unique = false)
        private String businessId;

        //Business Key
        @Column(name = "name", length = 100, nullable = false)
        private String name;

        //Business Key
        @Column(name = "surname", length = 100, nullable = false)
        private String surname;

        @Column(name = "updateDateTime", length = 50, nullable = false)
        private Timestamp updateDateTime;

        @Version
        private int version;

        @Column(name = "user", length = 100000)
        @Lob
        private byte[] user;

        @Column(name = "userXml")
        @Lob
        private String userXml;

        public UserRecord(String businessId, String name, String surname, Timestamp updateDateTime, byte[] user, String userXml) {
            this.businessId = businessId;
            this.name = name;
            this.surname = surname;
            this.updateDateTime = updateDateTime;
            this.user = user;
            this.userXml = userXml;
        }
    }

服务层代码片段为 之前

  record = new RiskMetricRecord(businessId, name, surname, updateDateTimeTimestamp, serialize(userToSave));

之后

 record = new RiskMetricRecord(businessId, name, surname, updateDateTimeTimestamp, serialize(userToSave), userToSave);

我的Jparepository是:

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserRepository extends CrudRepository<UserRecord, String> {

}

因此,当我尝试在JPA实体中添加字段@Lob String后将我的实体保存在数据库中时-没有错误消息,但是,在数据库中也没有更改。.

更多信息: 数据库表是

-- auto-generated definition
CREATE TABLE USERRECORD
(
  PR_KEY            VARCHAR2(255 CHAR) NOT NULL
    PRIMARY KEY,
  BUSINESSID VARCHAR2(100 CHAR),
  USER        BLOB,
  NAME    VARCHAR2(100 CHAR) NOT NULL,
  SURNAME     VARCHAR2(100 CHAR) NOT NULL,
  VERSION           NUMBER(10)         NOT NULL,
  UPDATEDATETIME    TIMESTAMP(3),
  USERXML     CLOB
)
/

已尝试: 1)

 @Lob
    @Column(name = "userXml", columnDefinition = "CLOB")
    private String userXml;

2 个答案:

答案 0 :(得分:0)

当我在应用程序中持久保存BLOB时,我尝试了byte [],但没有保存数据。然后,我将实体类中属性的数据类型更改为java.sql.Blob,从而解决了该问题。

在您的情况下,您正在使用CLOB,然后将属性的数据类型从String或byte []更改为java.sql.Clob。

false admin

因此,在持久实体之前,您必须将user和userXml属性的有效负载转换为Clob对象。

要创建Clob对象,可以使用Hibernate实用程序。

@Column(name = "user", length = 100000)
@Lob
private Clob user;

@Column(name = "userXml")
@Lob
private Clob userXml;

答案 1 :(得分:-1)

在JPA中,您可以轻松使用此处提到的TEXT属性,它将存储任意数量的数据。同样,无需手动定义长度。并且也删除@Lob,这是没有必要的。请记住,首先删除数据库中的列,然后重新创建以更新其定义。

@Column(name = "userXml", columnDefinition="TEXT")
private String userXml;