Hibernate session.get return null

I have a code like this:

Session session = HibernateSessionFactory.sessionFactory.openSession(); System.out.println("------------------" + session.get(User.class, (long) 10)); System.out.println("------------------" + session.createSQLQuery("SELECT * FROM diploma.tbl_users Where id = 10").addEntity(User.class).uniqueResult()); 

The first line returns null.
The second returned valid entry.

But if I change places:

 System.out.println("------------------" + session.createSQLQuery("SELECT * FROM diploma.tbl_users Where id = 10").addEntity(User.class).uniqueResult()); System.out.println("------------------" + session.get(User.class, (long) 10)); 

Both lines return the correct result:

This is my factory hibernation session:

 public class HibernateSessionFactory { public static SessionFactory sessionFactory = new Configuration().configure("/META-INF/hibernate.cfg.xml") .buildSessionFactory(); } 

Why session.get(User.class, (long) 10)) return null?

UPDATE hibernate configuration:

 <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/diploma</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="hibernate.connection.charSet">UTF-8</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="show_sql">true</property> <property name="format_sql">true</property> ......................................... <mapping class="edu.test.entities.User" /> .................................. </session-factory> 

User.java

 @Entity @Table(name = "tbl_Users") public class User extends BaseEntity { @NotEmpty @Column(name = "Name") private String name; @NotEmpty @Column(name = "Surname") private String surname; @NotEmpty @Column(name = "Login") private String login; @NotEmpty @Size(min=6, max=20) @Column(name = "Password") private String password; @NotEmpty @Column(name = "Email") private String email; @NotEmpty @Column(name = "Phone") private String phone; @ManyToOne @JoinColumn(name = "Role", nullable = false) private Roles role; // getters and setters 

Id field from base object

  @MappedSuperclass public class BaseEntity implements Serializable { @Id @Column(name = "id", unique = true, nullable = false) @GeneratedValue private Long id; 

UPDATE 2 The problem was matching the file @JoinColumn(name = "Role", nullable = false) private Roles role; . I pointed out that the role cannot be null, and the record that I tried to retrieve with identifier 10 has the null value of the foreign key. So I change nullable = true and it works.

+4
source share
1 answer

Hibernate implements a PoEA card template with an identifier where the Hibernate session acts as a card. When you call .addEntity() , the loaded objects become associated with the Hibernate session.

Then, when you call the Hibernate session acquisition method, it first checks the entity cache and returns an existing object, if present.

So, in the first expression, when you call get , the entity is not yet present on the entity map. In the second fragment, the object is cached using the .addEntity() method.

update . So the problem is that the link to the role is declared using nullable = false , and there is no such role in the database.

See also: http://martinfowler.com/eaaCatalog/identityMap.html

+2
source

Source: https://habr.com/ru/post/1446225/


All Articles