简体   繁体   English

创建SpringSource Tool Suite(STS)休眠模板

[英]Creating SpringSource Tool Suite (STS) Hibernate Template

i've created the Hibernate project using Spring Template Project. 我已经使用Spring Template Project创建了Hibernate项目。 Two domain objects, a JUnit test, app-context.xml and the persistence-context.xml were created. 创建了两个域对象,一个JUnit测试,app-context.xml和persistence-context.xml。 Now i noticed this line 现在我注意到这条线

<jdbc:embedded-database id="dataSource"></jdbc:embedded-database>

and assume that the following happens 并假设发生以下情况

  1. A default HQSL db is used 使用默认的HQSL db
  2. The two created models Order.java & Item.java will automatically created in memory tables T_ORDER and T_ITEM and the these will be mapped as per annotations on the objects. 创建的两个模型Order.java和Item.java将在内存表T_ORDER和T_ITEM中自动创建,并且这些模型将根据对象上的注释进行映射。 Inside the auto created classes one of the test methods is as follows 在自动创建的类中,测试方法之一如下

     @Test @Transactional public void testSaveAndGet() throws Exception { Session session = sessionFactory.getCurrentSession(); Order order = new Order(); order.getItems().add(new Item()); session.save(order); session.flush(); // Otherwise the query returns the existing order // (and we didn't set the parent in the item)... session.clear(); Order other = (Order) session.get(Order.class, order.getId()); assertEquals(1, other.getItems().size()); assertEquals(other, other.getItems().iterator().next().getOrder()); } 

Questions ... 问题...

  1. Am i correct to think that the in memory tables are created from the domain models (Order/Item), and mapped? 我是否正确认为内存表是根据域模型(订单/项目)创建并映射的? Therefore session.flush() synchronize the object to the physical (in memory table).... 因此session.flush()将对象同步到物理(在内存表中)。
  2. Are these tables auto mapped because if i do the following 这些表是否自动映射,因为如果我执行以下操作

     session.save(order); session.flush(); session.clear(); Order other = (Order) session .createQuery("from T_ORDER where ORDER_ID =: orderid") .setLong("orderid", order.getId()) .uniqueResult(); 

i get an exception... 我有一个例外...

org.hibernate.hql.ast.[B]QuerySyntaxException[/B]: \
T_ORDER is not mapped [from T_ORDER where ORDER_ID =: orderid]
............
............

if these tables are not mapped automatically, how is flushing working at the first place? 如果这些表没有自动映射,那么刷新首先如何工作?

Table creation is a feature of Hibernate (and other JPA proviers). 表创建是Hibernate(和其他JPA提供者)的功能。 It taking place when the application/test starts. 它在应用程序/测试开始时发生。 It has nothing to do with any query. 它与任何查询无关。 Even if you only start your test, with Hibernate running and configured, it can create the tables. 即使仅在运行和配置Hibernate的情况下开始测试,它也可以创建表。

If Hibernate create the tables, drop old once, and so on, depends on its configuration: the property: hibernate.hbm2ddl.auto is used what hibernate do if its starts. 如果Hibernate创建表,一次删除旧表,依此类推,则取决于它的配置:属性: hibernate.hbm2ddl.auto用于启动hibernate时所执行的操作。 For example the value update will add not existing tables and columns. 例如,值update将添加不存在的表和列。

More details can be found in the documentation . 可以在文档中找到更多详细信息。

Your Exception When you uses Hibernate and write hibernate query statements, then you have to use HQL and not SQL. 异常当您使用Hibernate并编写休眠查询语句时,则必须使用HQL而不是SQL。 -- The main difference is that HQL is based on the classes but not on the tables. -主要区别在于HQL是基于类而不是基于表的。 So in your case you must not use T_ORDER , but Order (the same is for the id, you need to use the property/field name, but not the column name). 因此,在您的情况下,您不能使用T_ORDER ,而应该使用Order (对于id来说,相同,您需要使用属性/字段名称,但不能使用列名称)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM