File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Validation -JSR303 and the fly likes Please somebody help me with JPA how to persist an entity which has default fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java JSRs » Validation -JSR303
Bookmark "Please somebody help me with JPA how to persist an entity which has default fields" Watch "Please somebody help me with JPA how to persist an entity which has default fields" New topic
Author

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

caleb momanyi
Ranch Hand

Joined: Jun 17, 2012
Posts: 43

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

Joined: Mar 07, 2010
Posts: 1632
    
    7

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.


[How To Ask Questions][Read before you PM me]
caleb momanyi
Ranch Hand

Joined: Jun 17, 2012
Posts: 43

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

Joined: Mar 07, 2010
Posts: 1632
    
    7

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please somebody help me with JPA how to persist an entity which has default fields
 
Similar Threads
OneToOne persisting two objects in a single transaction
Problem -EJB3+JPA+HIbernate
help with bean validation error in JPA when trying to persist an Entity
Hibernate @Embeddable Annotation
how to use JPA to save a list of objects