If you want to change the name of the table i.e, if you want to give a name of your choice to the table that
is going to get created instead of hibernate picking the name of the bean, then use @Entity (name="USER_DETAILS").
Instead of giving the name to Entity annotation, we can change the name using @Table annotation. For example
@Table(name="USER_DETAILS"). The difference between these two annotations is that using @Entity
we are giving/changing entity name, but by using @Table we are giving name only to the table that the entity
creates. Let's take an example to make this more clear:
@Entity(name="Entity_User_Details")
@Table(name="Table_User_Details")
Now, the table name in the databse would be Table_User_Details. But when hinernate generates
query using JPQL, then the query would be "Select * from Entity_User_Details".
We can as well change the name of the column by using @Column(name="USER_ID") annotation. We can use
the @Column annotation before the variable declaration or before the getter of the variable.
If you want to persist date using "new date()" then in the data base it stores data along with the time stamp. To
avoid time stamp getting stored in the database then use the annotation @Temporal(TemporalType.DATE)
@Transient annotation is used when you do not want a field or variable to be persisted.
@Lob annotation stands for large object. This will allow user to enter huge amount of data
@GeneratedValue annotation is used to automatically assign values to the column. In other words whenever a new record
is entered, we need not supply value for this particular column. Hibernate internally takes care of assigning value
to that column. This annotation takes in an attribute known as strategy. There are four different strategies for
GeneratedValue. They are:
- Auto: By default the strategy is Auto. Specifying strategy as auto means that we leave it for hibernate to decide how it generates a new value every time.
- Identity: Some of the databases like SQL server, MySql has this feature known as identity columns. So hibernate uses this feature to generate a new value.
- Sequence: Hibernate uses the sequence object in the database. Databases like Oracle has sequence feature where the sequence object.nextValue will pull the next value.
- Table: A separate table can be used to generate a new value and track all the values that have been generated.
package com.ram.dao; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Lob; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Transient; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; @Entity (name="USER_DETAILS") @Table (name="USER_DETAILS") public class UserDetails { @Id @Column(name="USER_ID") @GeneratedValue(strategy=GenerationType.AUTO) private int userId; private String userName; //@Temporal(TemporalType.DATE) will set the date without time stamp @Temporal(TemporalType.DATE) private Date dateJoined; private String Address; //@Transient tells the hibernate to ignore the description while persisting the data @Transient //@Lob stands for large object. This will allow user to enter huge amount of data @Lob private String description; public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Date getDateJoined() { return dateJoined; } public void setDateJoined(Date dateJoined) { this.dateJoined = dateJoined; } public String getAddress() { return Address; } public void setAddress(String address) { Address = address; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } @Column(name="USER_NAME") public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
No comments:
Post a Comment