休眠:将字符串映射到TEXT而不是VARCHAR

时间:2019-06-05 16:24:03

标签: java hibernate h2

我目前正在上课。这个想法是建立一个简单的类似Twitter的后端和前端。对于此项目,我们获得了带有三个Maven模块的预构建项目:

  • 商务(实际的API)
  • 持久性(模型)
  • 演示文稿(网站)

该项目使用h2作为休眠数据库,以简化开发。 prebuild的东西效果很好,但是我为您自己的服务定义了这个Post模型:

GetEncountersResponse ClientidEncountersDateGet (string clientid, string date, string startToken = null);

DBIidentified:

import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.json.bind.annotation.JsonbDateFormat;
import javax.persistence.*;
import java.util.Date;

@EqualsAndHashCode(callSuper = true)
@Entity
@Data
public class DBPost extends DBIdentified {

    private String message;
    private String author;

    @Temporal(TemporalType.TIMESTAMP)
    @JsonbDateFormat(value = JsonbDateFormat.TIME_IN_MILLIS)
    private Date creationDate;

    public DBPost() {
    }

}

StartupBean:

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public class DBIdentified {

    private long id;

    @Id
    @GeneratedValue
    public long getId() {
        return this.id;
    }

    public void setId(final long id) {
        this.id = id;
    }

}

当我尝试在数据库中的Buisness模块​​的Bean中插入一个Post时,出现以下错误消息:

@PostConstruct
    public void startup() {

        final DBPost firstPost = this.entityManager.find(DBPost.class, 1L);

        // only initialize once
        if (firstPost == null) {
            final DBPost post = new DBPost();

            post.setUrl("https://dl.gitea.io/gitea/1.8.2/");
            post.setAuthor("Gitea");
            post.setUrgent(false);
            post.setVersion("1.8.2");
            post.setCreationDate(new Date());
            post.setMessage("BUGFIXES\n" +
                    "\n" +
                    "    Fix possbile mysql invalid connnection error (#7051) (#7071)\n" +
                    "    Handle invalid administrator username on install page (#7060) (#7063)\n" +
                    "    Disable arm7 builds (#7037) (#7042)\n" +
                    "    Fix default for allowing new organization creation for new users (#7017) (#7034)\n" +
                    "    SearchRepositoryByName improvements and unification (#6897) (#7002)\n" +
                    "    Fix u2f registrationlist ToRegistrations() method (#6980) (#6982)\n" +
                    "    Allow collaborators to view repo owned by private org (#6965) (#6968)\n" +
                    "    Use AppURL for Oauth user link (#6894) (#6925)\n" +
                    "    Escape the commit message on issues update (#6901) (#6902)\n" +
                    "    Fix regression for API users search (#6882) (#6885)\n" +
                    "    Handle early git version's lack of get-url (#7065) (#7076)\n" +
                    "    Fix wrong init dependency on markup extensions (#7038) (#7074)\n");

            this.entityManager.persist(post);
        }
    }

我想我需要将数据库中的消息类型更改为Caused by: org.h2.jdbc.JdbcSQLException: Value too long for column "MESSAGE VARCHAR(255)": "STRINGDECODE('BUGFIXES\n\n Fix possbile mysql invalid connnection error (#7051) (#7071)\n Handle invalid administrator use... (790)"; SQL statement: TEXT之类。

但是所有用于注释LONGTEXT的方法都失败了:

  • private String message
  • @Type(type="text")
  • @Column(length= Integer.MAX_VALUE)
  • @Column(columnDefinition="CLOB")
  • @Column(columnDefinition="TEXT")
  • @Column(length=65535, columnDefinition="Text")

问题可能出在不同的模块上,而持久性模块是否包含在商务模块中?还是我只是在休眠方面做错了什么?

2 个答案:

答案 0 :(得分:0)

这是您需要的注释:

@Column(columnDefinition="TEXT")

答案 1 :(得分:0)

经过一番搜索,我得到了答案:

DBIdentified中,对getter进行了注释,在DBPost中,对字段进行了注释。字段和getter注释的混合不起作用,因为hibernate将搜索@Id注释并假定一切都与之相似。

所以解决方法是

DBIdentified

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public class DBIdentified {

    @Id
    @GeneratedValue
    private long id;

    public long getId() {
        return this.id;
    }

    public void setId(final long id) {
        this.id = id;
    }

}