这是我使用身份注释时的学生随身听
package mappingdemo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity(name="student")
public class Student {
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
int id;
String firstName;
String lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
这是主要班级
public class SetStudentDetails {
public static void main(String ar[])
{
SessionFactory sfac=null;
sfac=new Configuration().configure("hibernateconfig/rs.xml").buildSessionFactory();
Session session = sfac.getCurrentSession();
session.beginTransaction();
Student stu=new Student();
stu.setFirstName("abc23");
stu.setLastName("xyz");
System.out.println("-------------"+stu);
session.persist(stu);
System.out.println("after saving obj"+stu);
session.getTransaction().commit();
}
}
这是输出:-
-------------Student [id=0, firstName=abc23, lastName=xyz]
Hibernate:
insert
into
student
(firstName, lastName)
values
(?, ?)
after saving objStudent [id=14, firstName=abc23, lastName=xyz]
这里在调用persist()时为什么要运行插入查询,并且在提交时不会触发任何查询。
如果我在学生pojo中评论了@GeneratedValue(strategy = GenerationType.IDENTITY)并在主类中使用setId,例如-
package mappingdemo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity(name="student")
public class Student {
//@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
int id;
String firstName;
String lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
这是主要课程-
package mappingdemo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class SetStudentDetails {
public static void main(String ar[])
{
SessionFactory sfac=null;
sfac=new Configuration().configure("hibernateconfig/rs.xml")
.buildSessionFactory();
Session session = sfac.getCurrentSession();
session.beginTransaction();
Student stu=new Student();
stu.setId(17);
stu.setFirstName("abc23");
stu.setLastName("xyz");
System.out.println("-------------"+stu);
session.persist(stu);
System.out.println("after saving obj"+stu);
session.getTransaction().commit();
}
}
然后输出将是:-
-------------Student [id=17, firstName=abc23, lastName=xyz]
after saving objStudent [id=17, firstName=abc23, lastName=xyz]
Hibernate:
insert
into
student
(firstName, lastName, id)
values
(?, ?, ?)
这里将运行commit()插入。“不运行查询”意味着在我不使用@GeneratedValue(strategy = GenerationType.IDENTITY)的情况下,调用持久化()时没有查询正在运行。