• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

JPA where can I put my cardinalities

 
Ranch Hand
Posts: 210
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I make a loan application of books, it's relatively classic.
I have 7 tables: author, book, loan, damage, examplaire, borrower, user.

I chose to proceed in the following way: a class = a table.
I work with ORM: JPA and hibernate.
I annotated all my classes with: @Entity, @Id, @GeneratedValue.
I'm having problems with cardinalities: @OneToMany, @ManyToOne, @OneToOne and @ManyToMany.

my source code is in french but I annotated the translation of my classes / tables

here is some source code:








it is three tables / classes are in relation to each other by strong cardinalities.

author (0, n) ---------------------- (1,1) book (0, n) ----------- ---- (1,1) loan

auteur (0,n)----------------------(1,1) livre (0,n)---------------(1,1) emprunt

where can I put my cardinalities in my object model?
 
Bartender
Posts: 20924
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't start class names with a lower case letter. JPA may not handle them properly. Class names should ALWAYS start with an upper-case letter. Member names should always start with a lower-case letter. The Java tools assume that that's what you will be doing and some of them malfunction when you do not.

You put your entity relationship on the field definitions. When using property method access, that would be on the "get" method. Like so:






For best results, you should always annotate both source and target relationships, but you can annotate only the property whose relationship you actually intent to use.

Note that the second example here is using a compound foreign key.
 
Philippe Ponceblanc
Ranch Hand
Posts: 210
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Please don't start class names with a lower case letter. JPA may not handle them properly. Class names should ALWAYS start with an upper-case letter. Member names should always start with a lower-case letter. The Java tools assume that that's what you will be doing and some of them malfunction when you do not.



yes i anderstand ! a class always start with an upper-case letter !

Tim Holloway wrote:
You put your entity relationship on the field definitions. When using property method access, that would be on the "get" method. Like so:



I'm using Spring MVC, as I understand it, I need to put the data-level relationships of Spring web-mvc in order to display them in * .JSP (views).

Tim Holloway wrote:



for my sample : Authors(0,n)<--------------------->(1,1) Books (0,n) <----------------------> (1,1) loan



I admit that I have trouble understanding where you put your code, I understood that it is in the GET method ?


for my sample : Authors(0,n)<--------------------->(1,1) Books (0,n) <----------------------> (1,1) loan

table authors                                                        table book                                                  table loan
-id_authors                                                           - id_book                                                     - id_loan
- firstname                                                            - isbn                                                           - date_begin
- lastname                                                            - title                                                            - date_end
-...                                                                         - ...                                                              -....

I do not understand your second example, I know you bring attention to the latter because I live in Burgundy and there are good wines ....
can you tell me how to do with my example of managing borrowing books in a library?


Tim Holloway wrote:
For best results, you should always annotate both source and target relationships, but you can annotate only the property whose relationship you actually intent to use.


Sorry, but I do not understand at all, because what you say is abstract!

Tim Holloway wrote:
Note that the second example here is using a compound foreign key.


I know what is a  compound foreign key, but in your example, there are baths that I do not understand or I do not assimilate!


if you have the time, give me an example from my model of 3 tables described above
 
Tim Holloway
Bartender
Posts: 20924
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I took my examples from a major project of mine that has lots of related entities. Not all wine comes from France.

The JPA annotations don't directly relate to Spring web-mvc. Rather, they are used by the application's persistence code to return the model data that web-mvc can use. In my case, I use JSF, but the JPA part of the application would be identical, regardless.

This statement probably will not work:

The primary reason is that JPA Entity objects are JavaBeans, and not only are there rules about capitalization of bean class names and member names for JavaBeans, but also for the data access methods. In specific: the JavaBean property get method name consists of the property's name with its first character translated to uppercase and prefixed with the word "get" for non-boolean properties, and "is" for boolean properties.

So instead of "getauthors", JPA will be trying to locate a method named "getAuthors". Since Java is case-sensitive, it will fail.

You have specified List<authors> as your return type. Again, class names should always begin with an upper-case letter. Thus: List<Authors>. And actually it's usually better to give an entity its singular grammatical name, so better would be List<Author>. And a OneToOne relationship makes no sense for a property which can return a List, since a List implies OneToMany or ManyToMany.


Likewise, the mappedBy annotation attribute is attempting to reference a property of the target class, but you have specified a property whose name begins with an upper-case letter, which is incorrect.

So a more appropriate form of that method would look something like this:
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!