• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@Basic Annotation in JPA

 
Ramki Sukumaran
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can any one tell me why is @Basic Annotations is used. When i googled it, it says that when this annotation is configured for any attribute of a POJO, the column for that attribute will be created in the table at DB with default data type. The same behavior will happen even if we not mention @Basic annotation for any attribute of the POJO. And it says that fetching type and optional parameter matters for @Basic. And the fetching has two type namely "EAGER" and "LAZY" what do they mean by Eagerly fetching and lazily fetching. It would be great you could elaborate it with some examples.

Regards
Ramki S
 
Volodymyr Lysenko
Ranch Hand
Posts: 512
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

As I know @Basic is used to specify that simple property(like String, Integer,Boolean,byte[]) should be fetched either lazily or eagerly.
 
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
I use the @Basic annotation well.... never. Its essentially useless 99% of the time.

The @Basic annotation is applied to basic types like those listed above. You will almost always want to fetch these eagerly as there is really very few reasons to only fetch part of a row and since that is the default, there is no need to apply the annotation. As far as the optional attribute is concerned it is supposed to define a runtime requirement that the parameter be present (optional = false). Since optional = true is the default you would never need to specify this. Some providers will also use this for schema generation purposes to place a not null constraint on that column. That said all of this is also achieved with the nullable attribute of @Column which I do frequently use. The only difference is the optional attribute should theoretically throw an exception if its null before the query is ever actually made. However providers like Hibernate treat these 2 exactly the same so only the use of one or the other is required.

A valid example of when you might use basic is on a LOB


Now here you may not want or care about this large object and so you do not want it fetched from the database as part of the query. Realize though while the FetchType.EAGER must be honored the LAZY is merely a suggestion, the ORM provider may choose to load it anyway. Indeed this may often times be the case.

Note the following quote:

Lazy property loading requires buildtime bytecode instrumentation! If your persistent classes are not enhanced, Hibernate will silently ignore lazy property settings and fall back to immediate fetching.


In general defining lazy fetching types on basic types does not make sense. It is important on relationships however but that is a different topic.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic