为什么sql没有插入从用户输入获取的值而不是从另一个表中选择记录

时间:2013-06-25 14:03:44

标签: hibernate hql

事实:

  

通过从另一个数据库表中选择,可以使用HQL插入查询将记录插入到一​​个数据库表中。

我理解,但是当我们想要存储来自一个模型类的数据时,我们能做些什么呢? 是否可以将数据存储到我们通过HQL从用户端获取的表中,还是有其他选择?

我是新手,所以如果这个问题无关紧要,我很抱歉。

任何帮助将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您不需要HQL,只需要Session.save(model)

答案 1 :(得分:0)

对于来自Model类的数据,您最初需要在模型类中设置数据,然后使用session调用save(modelClassObject)。我将在此解释一个简单示例,假设您的模型类是具有属性sid和sname的Student然后将setvalues设置为student bean并将Student引用传递给save方法

SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session s=sf.openSession();
s.beginTransaction();
Student stu=new Student();
stu.setIs(101);
stu.setName("john");
s.save(stu);//This automatically persists the values of Student to the databases
s.getTransaction().commit();//Dont forget to commit transaction
s.close();//To close the Session