Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Please somebody help me with JPA how to persist an entity which has default fields  RSS feed

 
caleb momanyi
Ranch Hand
Posts: 43
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i have a mysql database with a customer table. The customer table has columns

name, phone, customerId(this is the primary key) and balance.

I have created entity classes and their corresponding session beans. When i try to create a new customer object and set the name and phone and try to persist it, i get an error javax.validation.ConstraintViolationException: Bean validation Constraint(s) violated while executing Automatic Bean Validation on callback event: 'prePersist'. Please refer to embedded ConstraintViolations for details
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you did not post any of your code so it's hard to give you specifics, but if I had to guess you are probably using validation annotations like @NotNull on your Entity and one of them if failing to validate. For the simple object your described the likely culprit is the id field. Remember the id is a persistent id so it will be null until the object has been persisted (unless you are assigning your own which is uncommon). I would remove any type of validation annotations from the id as these are typically managed by the persistence provider anyway. If that does not work you will need to figure out which validation is failing.
 
caleb momanyi
Ranch Hand
Posts: 43
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Bill for your quick reply. Here is the code for my entity class,
i have tried to remove annotation @NotNull but it still doesn't work


the DDL for my customer table.


and here is the Exception i get when i set the logging level to finest


 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the exception says you need to get the details of what constraint you are violating off the exception. I recommend reviewing the ConstraintValidation API

You will need to do something like this:



Since the root cause of your problem is really not ORM related I am moving this topic to the new JSR-303 forum.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!