close nav

close nav

Thread: Hibernate Annotations Questions

Forum : Hibernate Core and Annotations.   8 May, 2021
User Image
Sriman

Contributor

Joined: 15 Jul, 2016

Points: 1820

Threads: 1920

Posts: 293

Hibernate Annotations Questions

| Quote Date : 8 May, 2021    Views:698    

Few questions on Hibernate Annotations.
  • What are the minimal annotations we need to write on an Entity class to call it as a Persistable Entity class
  • If we dont write any annotations on an attribute of an Entity class what is the default annotation that will placed on it
  • If we dont want to persistence an attribute of Entity class while working with annotations what should we do
  • If the column name and attribute name is not matching how to map attribute to the column of the database
  • How many places we can write annotations on an Entity Class
  • How does hibernate identifies and reads the annotations if we write in multiple places
  • What changes we need in hibernate.cfg.xml to read annotations of the Entity class

Comments

User Image
Hariom

Newbie

Joined : 8 Oct, 2020

Points : 100

Threads: 0

Posts: 5

Re: Hibernate Annotations Questions

Reply Date : 9 May, 2021

#1.@Entity and @Id are minimal annotations on entity class to call it as a persistable entity class.
#2.@Basic is a default annotation on top of attribute of an entity class.
#3.if we don't want to persistence any attribute of entity class we must use @Transient annotation on top of attribute.
#4.if the column name and attribute name is not match then we need to use @Column annotation at top of attribute for ex..
@Column(name="product_name")
private String productName;
#5.we can write annotations on 2 places in the entity class....
??->on top of attribute.
??->on top of getter method.
???here framework people provide one more way to develop interface driven application.
#6.if we write annotation in multiple places in entity class then hibernate identify the annotations by looking @Id annotation position, if we use @Id at top of attribute then hibernate use all annotations from attributes and ignore getter method annotations and vice-versa?. lets understood with example...
#7.if we use annotations then we need to use "class" attribute and pass the fully Qualified name of entity class in mapping tag in hibernate.cfg.xml file like..
<mapping class="com.hibernate.entities.Product"/>

 

User Image
BsgoudA

Newbie

Joined : 7 Oct, 2020

Points : 100

Threads: 4

Posts: 4

Re: Hibernate Annotations Questions

Reply Date : 9 May, 2021

1.What are the minimal annotations we need to write on an Entity class to call it as a Persistable Entity classans
  ans:?The minimal annotations on entity class are @Entity and @Id
2.If we dont write any annotations on an attribute of an Entity class what is the default annotation that will placed on it
ans: ?if dont write any annotation on attribute by default @basic(fetchtype.eager="true")
3.If we dont want to persistence an attribute of Entity class while working with annotations what should we do
ans: ?if we dont want to persistence an attribute we apply @Transient annotation on that attribute
4.If the column name and attribute name is not matching how to map attribute to the column of the database
ans: ?if the column name of the database and attribute of entity class not same/matching than we need map by using @colum(name="columnName") on top of the attribute in entity clss
5.How many places we can write annotations on an Entity Class
ans: ?we can write annotations in 2 places on entity class 1.attribute level and 2.Accessor method level (getter method)
6.How does hibernate identifies and reads the annotations if we write in multiple places
ans: by looking @id if we annotated at attribute level than it ignore the method level annotations or if we annotated @id on method level it ignore attribute level annotations
7.What changes we need in hibernate.cfg.xml to read annotations of the Entity class  
 ans: instead map the resource we map the full qualified name of the entity class
 like below
    <mapping class="com.hibernateAnoo.entities.marchent"/>?

User Image
Alok24

Newbie

Joined : 17 Feb, 2021

Points : 100

Threads: 1

Posts: 16

Re: Hibernate Annotations Questions

Reply Date : 8 May, 2021

The minimal annotations we need to write on an Entity class to call it as a Persistable Entity class are @Entity & @Id .

@Basic is the default annotation that will placed on an attribute of an Entity class.

We use @Transient annotation for avoiding persistence an attribute of Entity class.

We use @Column(name = "column_nm") annotation, if the column name and attribute name is not matching.

We can write annotations on an Entity Class at two places, i.e. at attribute level & at getter method level.

If @Id annotation is placed on attributes level, then it performs the persistence operation on attributes and
if @Id annotation is placed on getter method level, then it performs the persistence operation on getter method level.

Instead of mapping resource we write mapping class in hibernate.cfg.xml to read annotations of the Entity class?.












User Image
Nagarjunas

Newbie

Joined : 15 Jun, 2020

Points : 100

Threads: 3

Posts: 4

Re: Hibernate Annotations Questions

Reply Date : 8 May, 2021


Q : what are the minimal annotations we need to write on an Entity class to call it as a Persistable Entity class
A : @Entity & @Id

Q : If we dont write any annotations on an attribute of an Entity class what is the default annotation that will placed on it
A : @Basic

Q : If we dont want to persistence an attribute of Entity class while working with annotations what should we do
A : use @Transient annotation on the attribute which we dont wanted to persistence ?

Q : If the column name and attribute name is not matching how to map attribute to the column of the database
A : Using @Column(name="")

Q : How many places we can write annotations on an Entity Class
 A : 2

Q : How does hibernate identifies and reads the annotations if we write in multiple places
A :  based on @Id annotation where we have written it

Q : What changes we need in hibernate.cfg.xml to read annotations of the Entity class
A :  Instead of using resource attribute we have to use class attribute which requires fqn of the entity class on mapping element 


Load More

No More Comments