Followers

Monday, July 3, 2017

Hiberante_Part_1

1)Hibernate Congiguration file explanation:
 -hibernate.cfg.xml


"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">



com.mysql.jdbc.Driver
jdbc:mysql://localhost/hibernatedb
suresh
suresh123

1

org.hibernate.dialect.MySQL5Dialect

org.hibernate.cache.NocacheProvider

true

update
//If I use "create" it always create new schema and insert data.






2)Where to define Entity name and Id name in class of hibernate?
Ans: Entity name we can define as : @Entity(name = "USER_DETAILS") public class UserDetails {} For "Id" Field name we can define two way: 1) @Id @Column(name = "USER_ID") private int userId; Or 2)above getter method @Id @Column(name = "USER_ID") public int getUserId() { return userId; } For generic Column also two way: 1)@Column(name = "USER_NAME") private String userName; Or 2)Abovet getter method @Column(name = "USER_NAME") public String getUserName() { return userName; }
  3)What is the role of java define "static"?
  Ans: It will not save the field column name or field value to Database. private static String description; public static String getDescription() { return description; } public static void setDescription(String description) { UserDetails.description = description; }
  4)What is AccessType is Hibernate? Ans: For best practice always use one type.There are two type of AccessType 1)FIELD (Default type) 2)PROPERTY If we mix both of them the 'transient' property will not work.So Alway use one type or Specify Access type.
5)What is behaviour of Data field in hibernate or use of '@Temporal'?
Ans: We can insert the Date field data in three Format:
 1)TimeStamp(This is default format, It saved both Date and time e.x: '2017-06-26 14:33:25'.) --> @Temporal(TemporalType.TIMESTAMP)
 2)Date(This will save only Date e.x: '2017-06-26'.) --> @Temporal(TemporalType.DATE)  3)Time(This will save only Time e.x: '14:34:37'.) --> @Temporal(TemporalType.TIME) Field Example: @Temporal(TemporalType.DATE) private Date joiningDate;
6)How to store lengthy string data or use of "@Lob"?
Ans: Define the field with annotation "@Lob".
 It has two kind of data storage:
 1)Byte stream (@Blob).
 2)Character Stream(@Clob).
7)What is Natural key & Surrogate key? 
Ans: Natural Key: They key which we can use as Primary key and having Business use. For example : emailId / mobileNumber/aadharcardNo/pan card number/SSN/username etc.
Surrogate key: Unique Key which is not having any Business use then we called Surrogate Key. for Example in Entity class we define we id field which is unique,auto increment etc. Annotation for generating Primary key as surrogate key: @Id @Column(name = "USER_ID") @GeneratedValue(strategy=GenerationType.AUTO) //GenerationType.IDENTITY or GenerationType.SEQUENCE or GenerationType.TABLE private int userId; GenerationType.AUTO:By default if we not specifty any strategy then it will be used.It's like increment by 1. GenerationType.IDENTITY: This is not a generic one.Some Database use Identity column featur for Primary key like mysql/sqlserver. GenerationType.SEQUENCE: Sequence object is use for Primary key like Oracle DB.Sequence object is used to matain the sequences. GenerationType.TABLE: A separate Table store the last value used as Primary Key and use next value for new record insertion.
  8)What is Value type & Embeded object?
Ans: The data which is not having any meaning of it's own.It's used by other object to have meaningful data. For example AnY user might contains a Address details.So for User Table Address details will be a Value object. User_details(Entity Object) Address(Value object) Embeded object can be define as: @Embeddable public class Address { } Use is main Entity class as: @Entity @Table(name = "USER_DETAILS") public class UserDetails { @Embedded private Address address; } It will Create all the column define in embedded Object also in main Entity class.
9)How to override Attribute Column name of Embedded object?
Ans: Refer entity class code:
 @Entity
 @Table(name = "USER_DETAILS")
 public class UserDetails {

@Embedded
 @AttributeOverrides({
@AttributeOverride(name = "street", column = @Column(name = "HOME_STREET_NAME")), @AttributeOverride(name = "city", column = @Column(name = "HOME_CITY_NAME")), @AttributeOverride(name = "state", column = @Column(name = "HOME_STATE_NAME")), @AttributeOverride(name = "pincode", column = @Column(name = "HOME_PIN_CODE")) })  private Address homeAddress;

@Embedded
 @AttributeOverrides({
 @AttributeOverride(name = "street", column = @Column(name = "OFFICE_STREET_NAME")),  @AttributeOverride(name = "city", column = @Column(name = "OFFICE_CITY_NAME")),  @AttributeOverride(name = "state", column = @Column(name = "OFFICE_STATE_NAME")),  @AttributeOverride(name = "pincode", column = @Column(name = "OFFICE_PIN_CODE")) }) private Address officeAddress;
 }
10)How to insert collection of Embeded Element into Entity?
Ans: This scenario occured when we do not know how many of Instance of Emebeded Object will be associated with main entity. For example : All the address One guy stayed while renewing the new passport.
 @Entity
 @Table(name = "USER_DETAILS")
 public class UserDetails {
 @ElementCollection
 Set listOfAddress = new HashSet();
 }
 In Test class we need to use like:
 Address a1 = new Address();
 a1.setStreet("This is my address");
 a1.setCity("Bangalore");
 a1.setState("Karnataka");
 a1.setPincode("560008");
 Address a11 = new Address();
 a11.setStreet("11-This is my address");
 a11.setCity("11-Bangalore");
 a11.setState("11-Karnataka");
 a11.setPincode("11-560008");
 user1.getListOfAddress().add(a1);
 user1.getListOfAddress().add(a11);

No comments:

Post a Comment