我有这样的代码:
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));
两行都返回正确的结果:
这是我的休眠会话工厂:
public class HibernateSessionFactory {
public static SessionFactory sessionFactory = new Configuration().configure("/META-INF/hibernate.cfg.xml")
.buildSessionFactory();
}
为什么session.get(User.class, (long) 10))
返回 null ?
更新 休眠配置:
<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>
用户.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)
private Roles role;
,我已指定角色不能为空,并且我尝试使用 id 10 检索的记录具有空角色外键。所以我改变 nullable = true
并且它起作用了。