我是休眠的新手,想编写一个休眠的一对多项目。我可以编译成功,但是控制台只有一句话:
Hibernate: select max(id) from customers.
你能帮我找出错误吗?
1)这是Customer.java
:
package com.test;
import java.util.Set;
public class Customer
{
private Long id;
private String name;
private Set orders;
public Customer(String name,Set orders)
{
this.name=name;
this.orders=orders;
}
public Customer(Set orders)
{
this.orders=orders;
}
public Customer(){}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getOrders() {
return orders;
}
public void setOrders(Set orders) {
this.orders = orders;
}
}
2)这是Order.java
:
package com.test;
public class Order
{
private Long id;
private String orderNumber;
private Customer customer;
public Order(String orderNumber,Customer customer){
this.orderNumber=orderNumber;
this.customer=customer;
}
public Order(){}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
3)这是Customer.hbm.xml
:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.test.Customer" table="customers">
<id name="id" column="id" type="long">
<generator class="increment">
</generator>
</id>
<property name="name" type="string">
<column name="name" length="15"></column>
</property>
<set name="orders" cascade="save-update" inverse="true">
<key column="customer_id"></key>
<one-to-many class="com.test.Order"/>
</set>
</class>
</hibernate-mapping>
4)这是Order.hbm.xml
:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.test.Order" table="orders">
<id name="id" column="id" type="long">
<generator class="increment">
</generator>
</id>
<property name="orderNumber" type="string">
<column name="order_number" length="15"></column>
</property>
<many-to-one name="customer" column="customer_id" class="com.test.Customer">
</many-to-one>
</class>
</hibernate-mapping>
5)这是hibernate.cfg.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:8889/mydata</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="Customer.hbm.xml"/>
<mapping resource="Order.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6)这是Test.java
:
package com.test;
import java.util.HashSet;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
public static SessionFactory sessionFactory;
static
{
try
{
sessionFactory=new Configuration().configure().buildSessionFactory();//Configuration,sessionFactory are all objects
}
catch(Exception ex)
{
System.out.println("exception occured");
ex.printStackTrace();
}
}
public static void saveCustomerAndOrderWithCascade() throws Exception
{
Session session=sessionFactory.openSession();
Transaction tx=null;
try
{
tx=session.beginTransaction();
Customer customer=new Customer("zhangsan",new HashSet());
Order order=new Order();
order.setOrderNumber("zhangsan_order1");
Order order2=new Order();
order.setOrderNumber("zhangsan_order2");
Order order3=new Order();
order.setOrderNumber("zhangsan_order3");
order.setCustomer(customer);
order2.setCustomer(customer);
order3.setCustomer(customer);
customer.getOrders().add(order);
customer.getOrders().add(order2);
customer.getOrders().add(order3);
session.save(customer);
tx.commit();
}
catch(Exception ex)
{
if(null!=tx)
{
tx.rollback();
}
}
finally
{
if(null!=session)
{
session.close();
}
}
}
public static void main(String[] args) throws Exception
{
saveCustomerAndOrderWithCascade();
}
}
控制台只有这些句子:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select max(id) from customers
有人能告诉我为什么它只有这个吗?谢谢!