我的类似乎被正确映射(如果我尝试持久化它在数据库中创建表的对象(如果它丢失了))并且我可以成功查询,但是我无法将新创建的实例保持为DB。
我在控制台中显示了SQL查询,并且它没有生成插入查询,但它之后尝试选择(最大)id
有人可以帮我找出原因吗?
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
User myUser = new User();
myUser.setUserId("Acccc");
myUser.setPassword("abc");
session.save(myUser);
和班级:
@Entity
@Table(name = "users")
@SuppressWarnings("serial")
public class User implements Serializable
{
//Properties
@Id
@Column
private int id;
@Column
private String userId;
@Column
private String password;
//Getters
public int getId() { return id; }
public String getUserId() { return userId; }
public String getPassword() { return password; }
//Setters
public void setId(int id) { this.id = id; }
public void setUserId(String userId) { this.userId = userId; }
public void setPassword(String password) { this.password = password; }
}
和config:
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="nz.co.genesis.healthsafety.stripes.model.User" table="users">
<id name="id" type="int" column="id" >
</id>
<property name="userId" type="java.lang.String" column="userId" not-null="false" length="45" />
<property name="password" type="java.lang.String" column="password" not-null="false" length="45" />
</class>
</hibernate-mapping>
和DB:
id : PK NN AI
userId varchar45, default NULL
password varchar45, default: null
答案 0 :(得分:1)
这是我的测试hibernate模型的代码
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
props.setProperty("hibernate.connection.driver_class", "com.microsoft.jdbc.sqlserver.SQLServerDriver");
props.setProperty("hibernate.connection.url", "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=databaseName");
props.setProperty("hibernate.connection.username", "username");
props.setProperty("hibernate.connection.password", "password");
props.setProperty("hibernate.show_sql", "true");
props.setProperty("hibernate.transaction.flush_before_completion", "true");
props.setProperty("hibernate.connection.release_mode", "auto");
props.setProperty("hibernate.transaction.auto_close_session", "true");
Configuration cfg = new Configuration();
cfg.setProperties(props);
// add dependency table class
cfg.addClass(ComputerModel.class);
cfg.addClass(CpuModel.class);
cfg.addClass(CdRomModel.class);
cfg.addClass(BrandModel.class);
SessionFactory sessions = cfg.buildSessionFactory();
Session ss = sessions.openSession();
List<ComputerModel> list = (ss.createQuery("from ComputerModel ")).list();
System.out.println("Found : " + list.size() + " items.");
System.out.println("======================================");
for (int i = 0; i < list.size(); i++) {
ComputerModel result = (ComputerModel) list.get(i);
System.out.println("computerSerialNo: " + result.getComputerSerialNo() +
", " + result.getCpuModel());
}
ss.close();
sessions.close();
}
答案 1 :(得分:0)
我认为你没有配置Transaction Hibernate