Hibernate Validator:hbm2ddl忽略EmbeddedId约束

Tim*_*Tim 5 validation hibernate hbm2ddl

这里有一个相当具体的问题,但它现在
让我烦恼了一天:我在PostgreSQL 8.3上使用Hibernate Core,Annotations&Validator.

我有以下类设置:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Entry {
    @EmbeddedId
    protected EntryPK       entryPK;
    @ManyToMany
    private Set<Comment>    comments    = new HashSet<Comment>();
...

@Embeddable
public class EntryPK implements Serializable {
    @ManyToOne(cascade = CascadeType.ALL)
    private Database    database;

    @Length(max = 50)
    @NotEmpty
    private String      pdbid;
...
Run Code Online (Sandbox Code Playgroud)

我想看看长度约束在我的PostgreSQL数据库中转换为长度约束(它适用于@ Entity中的其他字段,而不是@ Embeddable's),但它似乎并不想工作..
甚至使用@IdClass代替@EmbeddedId并在@Entity中的匹配字段上应用Length约束并没有解决这个问题:数据库字段仍然是varchar 255(大约250太大了,不能满足我的需要).
有些人可能会说我不应该关心这个详细程度,但我的OCD方面拒绝放手......;)是不是可以在EmbeddedId中使用Hibernate Validator Annotations并让hbm2ddl将约束应用于数据库字段?

sou*_*frk 0

不是答案。经历同样的行为。请作者识别以下代码是否与问题陈述相符。

实体和复合 ID 类。

@Embeddable
public class MyComposite implements Serializable {
    private static final long serialVersionUID = 5498013571598565048L;

    @Min(0)
    @Max(99999999)
    @Column(columnDefinition = "INT(8) NOT NULL", name = "id", nullable = false)
    private Integer id;

    @NotBlank
    @NotEmpty
    @Column(columnDefinition = "VARCHAR(8) NOT NULL", name = "code", length = 8, nullable = false)
    private String code;
    // plus getters & setters.
}

@Entity
@Table(name = "some_entity_table")
public class MyEntity {
    @EmbeddedId
    private MyComposite composite;

    public MyComposite getComposite() {
        return composite;
    }

    public void setComposite(MyComposite composite) {
        this.composite = composite;
    }
}
Run Code Online (Sandbox Code Playgroud)

课程的单元测试

@Test
public void createWithIdOutOfRangeTest(){
    Exception exception = null;
    MyEntity input = new MyEntity();
    MyEntity output = null;
    MyComposite id = new MyComposite();
    // EITHER THIS
    id.setId(123456789);
    id.setCode("ABCDEFG");
    // OR THIS
    id.setId(12345678);
    id.setCode("        ");
    input.setComposite(id);
    try {      
      output = service.create(input);
    } catch (Exception e) {
      exception = e;
    }
    Assert.assertNotNull("No exception inserting invalid id !!", exception);
    Assert.assertTrue("There was some other exception !!", exception instanceof ConstraintViolationException);
}
Run Code Online (Sandbox Code Playgroud)

Hibernate-core:5.0.12正如问题中所述,将无效值传递给复合键字段( 、 )时,我没有遇到任何异常H2:1.4.196。测试失败。