0

Here is the student pojo when I use Identity annotation

            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 + "]";
            }

            }

and here is the main class

    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();
            }
        }

This is the output:-

        -------------Student [id=0, firstName=abc23, lastName=xyz]
        Hibernate: 
            insert 
            into
                student
                (firstName, lastName) 
            values
                (?, ?)
        after saving objStudent [id=14, firstName=abc23, lastName=xyz]

here on calling persist() why insert query is running and on commit no query will be fired.

if I commented @GeneratedValue(strategy=GenerationType.IDENTITY) in student pojo and use setId in main class like-

            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 + "]";
                }

            }

This is main class-

            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();
                }

            }

then output will be:-

            -------------Student [id=17, firstName=abc23, lastName=xyz]
            after saving objStudent [id=17, firstName=abc23, lastName=xyz]
            Hibernate: 
                insert 
                into
                    student
                    (firstName, lastName, id) 
                values
                    (?, ?, ?)

here on commit() insert will run.'no query will be run' means on calling persist() no query is running in this scenario when i don't use @GeneratedValue(strategy=GenerationType.IDENTITY).

  • Can you share both type of POJO class code.. i.e. with `GenerationType.IDENTITY` and `seID` ?? – miiiii Sep 04 '18 at 10:57
  • What do you mean with "no query will be run", i can see a query in the Log. The persist does not force a direct Insert statement. The JPA implementation decides when an object of the persistence context will be finally flushed to the DB. You log statement is before the commit. This might also help [when-does-the-jpa-set-a-generatedvalue-id](https://stackoverflow.com/questions/9087848/when-does-the-jpa-set-a-generatedvalue-id) – Andre Albert Sep 04 '18 at 11:13

0 Answers0