Hibernate 验证器:hbm2ddl 忽略了 EmbeddedId 约束
这是一个相当具体的问题,但它已经困扰我一天了:
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是答案。 经历同样的行为。 请作者识别以下代码是否与问题陈述相符。
实体与 复合 ID 类。
类的单元测试
正如问题中所述,将无效值传递给复合键字段时我没有遇到任何异常(
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.
Unit tests for the classes
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.