File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JPA Certification (OCEJPA) and the fly likes JPA Mapping Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » JPA Certification (OCEJPA)
Bookmark "JPA Mapping Exception" Watch "JPA Mapping Exception" New topic
Author

JPA Mapping Exception

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9629
    
    2

Guys,

I have a simple JPA mapping to a couple of tables via the XML config file. However, during startup, I get the following errors:



Below is how my mapping looks like: A Question has many Answers:


What is the mistake here?

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9629
    
    2

Modified the definitions like below:



And following is the exception:

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9629
    
    2

Obviously no reason why Annotation mapping makes life easier.
Tamas Polgar
Greenhorn

Joined: Apr 27, 2011
Posts: 13
Hi,
using hibernate as jpa2 provider could produce wierd situations

For your first problem:
If you change the order of your attribute mappings in Answer, and put <many-to-one name="question"/> to the last statement, it should pass the XMLSchemaValidation. It shouldnt be a problem with a jpa2 provider, but with hibernate, it is.
You use <one-to-many name="answers" mapped-by="answerId"> in your Question, this should be mapped by an attribute with Question type, with many-to-one relation type.

For your second problem
and

are not good, you cannot use one-to-many or many-to-one in the id tag.
Try to put them outside the id, and sculp it a bit

How does the datamodel look like?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9629
    
    2

The data model is quite simple. A Question has many Answers and it is a simple One-To-Many bidirectional relationship.

In my first post above, I had the Ont-To-Many definition outside of the id attribute and it did not work either.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9629
    
    2

Seems like I have to change to Annotation mapping as this is not moving any further with my development.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9629
    
    2

Tried to get rid of all the mapping errors. But now getting an all together new exception....



Where Technology is an Entity as defined below:



Are sequences not supported in MySQL database?
Tamas Polgar
Greenhorn

Joined: Apr 27, 2011
Posts: 13
There are no sequences in mysql,
you can use id generation (when you insert the record, it generates an id, dont use it for complex relationships), or table generator which is quite cool (details).

so I created a simple version of your stuff
Answer:

xml for it:

Question

xml:

sql script:

It works for me, with hibernate as well.
Note:
many-to-one and one-to-many are the last elements of the mappings. with hibernate it seems to be required..
You dont need to use basic if you use default values (other attribute = other column). Try to use default values, it can reduce the code, and improves the readability.

you can use annotation or xml, or both. you can specify the same mapping with xml and annotations (in theory ).
however annotations could be easier to understand how jpa works.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9629
    
    2

Thanks for the post. It deploys fine now.
 
 
subject: JPA Mapping Exception