java – hibernate session.get返回null

我有这样的代码:

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

第一行返回null.
第二次返回有效记录.

但如果我改变地方:

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

两行都返回正确的结果:

这是我的hibernate会话工厂:

public class HibernateSessionFactory {

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

 }

为什么session.get(User.class,(long)10))返回null?

UPDATE
hibernate配置:

<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字段

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

更新2
  问题出在映射文件@JoinColumn(name =“Role”,nullable = false)
    私人角色角色;我已指定Role不能为null,并且我尝试使用id 10检索的记录具有null Role外键.所以我改变了nullable = true并且它有效.

最佳答案 Hibernate实现了
Identity Map PoEAA pattern,其中Hibernate会话扮演地图的角色.当您调用.addEntity()时,加载的实体将与Hibernate会话关联.

然后,当您调用Hibernate会话的get方法时,它首先检查实体缓存并返回现有实体(如果存在).

因此,在第一个语句中,当您调用get时,实体尚未出现在实体映射中.在第二个片段中,实体正在使用.addEntity()方法进行缓存.

更新所以问题是对该角色的引用是用nullable = false声明的,并且在数据库中没有这样的角色.

另见:http://martinfowler.com/eaaCatalog/identityMap.html

点赞