Reputation: 16420
my classes seem to be mapped correctly (if I try persist an object it creates the table in the DB (if it's missing)) and I can query succesfully, but I can't get a newly created instance to be persisted to the DB.
I have show SQL queries in console, and it's not generating the insert query, but it's trying to select(max) id afterwards
Could someone please help me figure out why?
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
User myUser = new User();
myUser.setUserId("Acccc");
myUser.setPassword("abc");
session.save(myUser);
and class:
@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; }
}
and 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>
And DB:
id : PK NN AI
userId varchar45, default NULL
password varchar45, default: null
Upvotes: 0
Views: 387
Reputation: 403
This is my code for test hibernate model
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();
}
Upvotes: 1