Hibernate Validator: EmbeddedId constraint ignored by hbm2ddl
Asked Answered
D

1

6

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?

Defaulter answered 3/4, 2009 at 9:45 Comment(2)
Just a quick follow up: Ended up going for a different implementation (using generated IDs (so yes, I surrendered ;)), but I'm guessing this might be a candidate for a feature request / bug report to the hibernate-validator project..Defaulter
I think I might be having the same problem. Requesting @Defaulter to review the code posted underneath.Osbert
O
0

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.

Osbert answered 10/8, 2017 at 8:0 Comment(1)
Thanks for the extended example here; Can't say for certain if this is the same issue, as it's been eight years and lot's of hibernate updates since. :) Along the way I've picked up Flyway to manage my database schema and migrations; Perhaps that could be of use to you too.Defaulter

© 2022 - 2024 — McMap. All rights reserved.