Hibernate程序运行到org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory 停止

如果一切再重来 2015-05-18 09:52:53
<hibernate-configuration>
<session-factory>

<!-- 配置连接数据库的基本信息 -->
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql:///hibernate
</property>
<!-- 省略默认 写法 <property name="hibernate.connection.url"> jdbc:mysql:///book</property> -->
<property name="connection.username">root</property>
<property name="connection.password">mysqlROOT</property>


<!-- 配置Hibernate的基本信息 -->

<!-- 配置Hibernate的方言 为MySQLInnoDBDialect, 在hibernate.properties文件中可以找到常用的配置
如果改成MySQLInnoDBDialect则不能自动建表不知道为什么?? -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- 执行操作时是否在控制台打印 SQL语句 -->

<property name="hibernate.show_sql">true</property>

<!-- 是否对SQL语句进行格式化 -->

<property name="hibernate.format_sql">true</property>

<!-- 指定自动生成数据表的策略 -->

<property name="hibernate.hbm2ddl.auto">update</property>

<!-- 指定关联的 .hbm.xml文件 -->

<mapping resource="com\xiaoyabin\hiberbate\pojo\Person.hbm.xml" />

</session-factory>
</hibernate-configuration>



person.hbm.xml

<hibernate-mapping>
<class name="com.xiaoyabin.hiberbate.pojo.Person" table="t_person">

<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- native表示底层数据库自动生成标识符 -->
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<property name="birthday" type="java.util.Date">
<column name="BIRTHDAY" />
</property>

<!-- Array数组的映射配置,效率低,通常不建议使用 -->
<array name="my_Array" table="t_array">
<!-- key 元素:指定集合属性对应的表的外键列, 指定t_array表的数据与与t_person表的数据的关系 -->
<key column="array_id" not-null="true"></key>
<!-- 数组是有序的,需要保存数据的顺序值,list-index 元素:指定索引列 -->
<list-index column="indexs" base="0"></list-index>
<element column="array_value" type="java.lang.String"></element>
</array>

<!-- List的映射配置 -->
<list name="my_List" table="t_list">
<!-- key 元素:指定集合属性对应的表的外键列, 指定t_list表的数据与与t_person表的数据的关系 -->
<key column="list_id" not-null="true"></key>
<!-- 数组是有序的,需要保存数据的顺序值,list-index 元素:指定索引列 -->
<list-index column="indexs" base="0"></list-index>
<element column="list_value" type="java.lang.String"></element>
</list>

<!-- Map的映射配置 -->
<map name="my_Map" table="t_map">
<!-- key 元素:指定集合属性对应的表的外键列, 指定t_map表的数据与与t_person表的数据的关系 -->
<key column="map_id" not-null="true"></key>
<!-- 存储map的key值,而且map不需要指定顺序,只有key值 -->
<map-key column="key_id" type="java.lang.Integer"></map-key>
<element column="map_value" type="java.lang.String"></element>
</map>

<!-- Set的映射配置,set是无序的,无需list-index属性 -->
<set name="my_Set" table="t_set">
<!-- key 元素:指定集合属性对应的表的外键列, 指定t_set表的数据与与t_person表的数据的关系 -->
<key column="set_id" not-null="true"></key>
<element column="set_value" type="java.lang.String"></element>
</set>


</class>
</hibernate-mapping>



测试代码:
@Before
public void setUp() throws Exception {
System.out.println("------setUp---初始化测试资源-----");
Configuration config = new Configuration().configure();
ServiceRegistry sr = new StandardServiceRegistryBuilder()
.applySettings(config.getProperties()).build();
sessionFactory = config.buildSessionFactory(sr);
session = sessionFactory.openSession();
}

@Test
public void testAdd() {

Person p = new Person();
p.setName("tom2");
p.setAge(12);
p.setBirthday(new java.util.Date());

String[] array = { "A", "B", "C" };
p.setMy_Array(array);

ArrayList<String> list = new ArrayList<String>();
list.add("List!");
list.add("映射");
list.add("测试!");
p.setMy_List(list);

HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "Map!");
map.put(2, "映射!");
map.put(3, "测试!");
p.setMy_Map(map);

HashSet<String> set = new HashSet<String>();
set.add("Set!");
set.add("映射!");
set.add("测试!");
p.setMySet(set);

tx = session.beginTransaction();
session.persist(p);
tx.commit();

}

@Test
public void testGet() {
Person p = (Person) session.get(Person.class, 2);
System.out.println(p);
}

/**
* 释放测试数据
*
* @throws Exception
*/
@After
public void tearDown() throws Exception {
System.out.println("------tearDown---释放测试数据---");
session.close();
sessionFactory.close();
}

Person类:
public class Person {

private Integer id;
private String name;
private int age;
private Date birthday;

private String[] my_Array;
private List my_List;
private Map my_Map;
private Set my_Set;

public String[] getMy_Array() {
return my_Array;
}

public void setMy_Array(String[] my_Array) {
this.my_Array = my_Array;
}

public List getMy_List() {
return my_List;
}

public void setMy_List(List my_List) {
this.my_List = my_List;
}

public Map getMy_Map() {
return my_Map;
}

public void setMy_Map(Map my_Map) {
this.my_Map = my_Map;
}

public Set getMySet() {
return my_Set;
}

public void setMySet(Set mySet) {
this.my_Set = mySet;
}

public Person(String name, int age, Date birthday, String[] my_Array,
List my_List, Map my_Map, Set my_Set) {
super();
this.name = name;
this.age = age;
this.birthday = birthday;
this.my_Array = my_Array;
this.my_List = my_List;
this.my_Map = my_Map;
this.my_Set = my_Set;
}

public Person() {
}

@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age
+ ", birthday=" + birthday + "]";
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

}

请问是为什么呀,小弟初学Hibernate
...全文
289 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
看了好久,终于找到问题了,一个低级错误,忘了给MY_SET指定SETget方法

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧