3

I have a Class having two fields

@Column(name = "transactionDate", nullable = true)
private Date transactionDate;

@Column(name = "nextTransactionDate", nullable = true)
private Date nextTransactionDate

--

Both fields can be null. But if transactionDate is not null, then nextTransactionDate must not be null. How can I implement the above relationship between the fields using JPA? Any code snippet, link would be appreciated.

Note: I am using JPA not Hibernate.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Rafiq
  • 2,000
  • 2
  • 21
  • 27
  • 2
    You need to use a the bean validation API to do this, have a look at [this](http://stackoverflow.com/questions/9804009/jsr-303-bean-validation-annotate-multiple-fields) – Boris the Spider Mar 05 '13 at 14:22
  • 1
    @bmorris591 It is only object or application level validation not database constraints. – Rafiq Mar 05 '13 at 16:24

2 Answers2

2

You could validate this in your object model or application instead of through database constraints. You may also be able to define your own check constraint in the database through your own DDL script.

James
  • 17,965
  • 11
  • 91
  • 146
  • I do not know which database my boss will be used. So I want a generalize form so that I can switch from one db to another. – Rafiq Mar 05 '13 at 16:14
0

It's better to code the biz logic in DAO or service layer instead of defining it in ORM level. JPA and ORM tools is just designed to resolve persistence stuff not biz logic stuff.

Henry Leu
  • 2,184
  • 4
  • 22
  • 34