MySQLに新しいレコードを挿入したいと思います。 データベース:enter image description here エンティティクラス: `package entites; //生成日2011年8月11日1:32:55 PM by Hibernate Tools 4.3.1HQLクエリで挿入
import java.util.Date;
/** * UserMonhoc */ パブリッククラスUserMonhocでjava.io.Serializable {
private UserMonhocId id;
private Monhoc monhoc;
private User user;
private Date thoigianDk;
public UserMonhoc() {
}
public UserMonhoc(UserMonhocId id, Monhoc monhoc, User user, Date thoigianDk) {
this.id = id;
this.monhoc = monhoc;
this.user = user;
this.thoigianDk = thoigianDk;
}
public UserMonhocId getId() {
return this.id;
}
public void setId(UserMonhocId id) {
this.id = id;
}
public Monhoc getMonhoc() {
return this.monhoc;
}
public void setMonhoc(Monhoc monhoc) {
this.monhoc = monhoc;
}
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
public Date getThoigianDk() {
return this.thoigianDk;
}
public void setThoigianDk(Date thoigianDk) {
this.thoigianDk = thoigianDk;
}
}を実装hbm2javaによって生成
`
コード:
public boolean InsertStudent(String idUS, String idMH) {
try {
sf.getCurrentSession().beginTransaction();
UserMonhoc umh = new UserMonhoc();
String hql2 = "INSERT INTO User_Monhoc(user_id,mh_id) VALUES('" + idUS + "','" + idMH + "')";
System.out.println(hql2);
Query query = sf.getCurrentSession().createSQLQuery(hql2);
query.executeUpdate();
return true;
} catch (Exception e) {
return false;
}
}
それは動作しません。 ありがとうございます。
エンティティクラスを表示し、正確なエラーメッセージを表示する必要があります。 – davidxxx
更新されました! –
どうしてなぜですか?ちょうど 'session.persist(umh)'を使ってください。 SQLクエリを使用してデータベースにデータを挿入する場合は、HibernateではなくJDBCを使用してください。 –