@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
By using @GeneratedValue annotation we can create primary key in table.The annotations takes 4 parameter as strategy attribute.
@GeneratedValue(strategy=GenerationType.AUTO)
Auto tells the Hibernate to do the primary key addition from hibernate side
@GeneratedValue(strategy=GenerationType.IDENTITY)
IDENTITY tells the Hibernate to do the primary key addition from DB side when set to auto increment.
@GeneratedValue(strategy=GenerationType.SEQUENCE)
SEQUENCE tells the Hibernate to do the primary key addition as defined by sequence in DB.
@GeneratedValue(strategy=GenerationType.TABLE)
TABLE tells the Hibernate to create a seperate table in DB and maintain the last inserted values.
The table contains only one row with last inserted value.
In Some cases the hibernate.cfg.xml might become unrecognized.In such case the code should be changed to force the config to be picked from the file location.
public static SessionFactory createSessionFactory()
{
Configuration configuration = new Configuration().configure();
configuration.configure("hibernate.cfg.xml");
configuration.addAnnotatedClass(com.mugil.tutor.UserDetails.class);
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
Hibernate uses SessionFactory pattern internally as below
1.Create Object for SessionFactory
2.Open Session to begin Transaction
3.Begin Transaction using beginTransaction() Method
4.Save the Object by Passing Object of the bean
5.Complete the Transaction using commit
Annotations
@Entity – Means entity as a whole>table would be created by the Name of the Entity
@Id – Tells the Primary Key
Having a Different table name from Class Name Annotations
@Entity(name="User_Details")
public class Users
{
.
.
}
Table with User_Details would be created instead of Users
Having a Different Column name from Object Name Annotations
@Entity(name="User_Details")
public class Users
{
@Id
@Column(name="USER_ID")
private String UserId;
.
.
}
Columns with User_Id would be created instead of UserId
Appending String to Getters
public void setName(String name)
{
Name = name + " Append Test ";
}
Appending String to Getters
@Entity
@Table (name="User_Details")
public class Users
{
}
The Entity Name Still remains the same but the table Name is different.
@Basic Annotation – Tells Hibernate to persist which it does by default
public class Users
{
@Basic
private String UserName;
.
.
}
@Basic has 2 Parameters – Fetch, optional. The only time you use @Basic is while applying the above options.
@Transient Annotation – Tells Hibernate to not store data in database
public class Users
{
@Transient
private String UserName;
.
.
}
@Temporal Annotation – Tells Hibernate to specify Date or Time
public class Users
{
@Temporal (TemporalType.Date)
private String joinedDate;
.
.
}
Without @Temporal the joinedDate is store along with TimeStamp in DB. Now using TemporalType(which is ENUM) you can select the type of data which can be stored in Database.
@Lob – Tells Hibernate to specify Date or Time
public class Users
{
@Lob
private String Address;
.
.
}
Tells the database field should be created as CLOB instead of VARCHAR(255).
For setting the Bean value in struts-config.xml we should use the form-beans tag.The form-beans tag might contain any number of bean property defined within form-bean as below.
In the first line I am defining action path as showBeanValue and it is going to forward to page showBeanValue.jsp when some one access the showBeanValue.do in url.
In the second statement I am creating a dummy form since the the html form action should not be empty.
public class Iterator extends ActionSupport
{
Map nmap = new HashMap<String, List<Person>>();
public String execute()
{
Person objPerson = new Person();
objPerson.userName = "Name1";
objPerson.age = 25;
List<Person> myList = new ArrayList<Person>();
myList.add(objPerson);
nmap.put("Mugil", myList);
objPerson.userName = "Name2";
objPerson.age = 26;
List<Person> myList2 = new ArrayList<Person>();
myList2.add(objPerson);
nmap.put("Mani", myList2);
objPerson.userName = "Name3";
objPerson.age = 27;
List<Person> myList3 = new ArrayList<Person>();
myList3.add(objPerson);
nmap.put("Vinu", myList3);
setNmap(nmap);
return SUCCESS;
}
public Map getNmap()
{
return nmap;
}
public void setNmap(Map nmap)
{
this.nmap = nmap;
}
}
Person Bean Code
class Person
{
String userName;
int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
This warning is generated since you have specified formName.action in action attribute of form.You get get rid of those by changing the action attribute to formName instead of formName.action