Hibernate 验证器:hbm2ddl 忽略了 EmbeddedId 约束

发布于 2024-07-15 19:53:30 字数 899 浏览 6 评论 0原文

这是一个相当具体的问题,但它已经困扰我一天了:
我正在使用 Hibernate Core、Annotations & PostgreSQL 8.3 上的验证器。

我有以下类设置:

@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;
...

我希望看到长度约束转换为我的 PostgreSQL 数据库中的长度约束(它适用于 @Entity 而不是 @Embeddable 内的其他字段),但它似乎不想工作。 .
即使使用@IdClass而不是@EmbeddedId并对@Entity中的匹配字段应用长度约束也没有解决这个问题:数据库字段仍然是varchar 255(大约250对于我的需要来说太大了)。
有些人可能会说我不应该关心这种程度的细节,但我的强迫症方面拒绝放手..;)是否不可能在 EmbeddedId 中使用 Hibernate Validator Annotations 并让 hbm2ddl 将约束应用到数据库领域?

Fairly specific question here, but it's been bugging me for a day now:
I'm using Hibernate Core, Annotations & Validator on PostgreSQL 8.3.

I have the following classes setup:

@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;
...

I'd like to see the Length constraint translated to a length contraint in my PostgreSQL database (which is working for other fields inside @Entity's rather than @Embeddable's) but it just doesnt seem to want to work..
Even using an @IdClass instead of @EmbeddedId and applying the Length constraint on the matching field in the @Entity did not fix this problem: the database field is still varchar 255 (about 250 too large for my needs).
Some might say I shouldn't care about this level of detail, but my OCD side is refusing to let it go.. ;) Is it just not possible to use Hibernate Validator Annotations inside an EmbeddedId and have hbm2ddl apply the constraints to the database fields?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私藏温柔 2024-07-22 19:53:30

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

实体与 复合 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;
    }
}

类的单元测试

@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);
}

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

Not an answer. Experiencing the same behaviour. Request the author to recognize if the following code aligns to the problem statement.

Entity & composite-id classes.

@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;
    }
}

Unit tests for the classes

@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);
}

And as stated in the question, I don't get any exception passing invalid values to composite key fields (Hibernate-core:5.0.12, H2:1.4.196). The test fails.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文