Hibernate Validator: EmbeddedId constraint is ignored by hbm2ddl

An extremely specific question here, but he listened to me for a day:
I use Hibernate Core, Annotations and Validator on PostgreSQL 8.3.

I have the following class settings:

@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 would like the length limit to be translated to the length limit in my PostgreSQL database (which works for other fields inside @Entity, not @Embeddable), but it just doesn't seem to work.
Even using @IdClass instead of @EmbeddedId and applying the length limit in the corresponding field in @Entity did not fix this problem: the database field is still varchar 255 (about 250 is too big for my needs).
Some may say that I should not care about this level of detail, but my OCD side refuses to let it go ..;) Can't use the Hibernate Validator annotations inside the EmbeddedId and hbm2ddl to apply restrictions to the field database?

+4
source share
1 answer

Not an answer. To experience the same behavior. Ask the author to find out if the following code matches the problem operator.

Types of objects and compound identifiers.

 @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 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 get no exceptions that pass invalid values ​​to the composite key fields ( Hibernate-core:5.0.12 , H2:1.4.196 ). The test does not work.

0
source

All Articles