标签:style http io ar os java sp for strong
请教问题如下:
(下面是进行的双向的多对多的关联关系进行拆分的测试,拆分成两个多对一的关联,但是没有成功的一步就是:只是生成了两张表,还有一张ORDER表没有生成,请教原因还修改的方式,谢了)
【POJO类】
package qau.edu.ren;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
public class Order {
private int id ;
private Date date ;
private String name ;
private Set<Op> ops = new HashSet<Op>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Op> getOps() {
return ops;
}
public void setOps(Set<Op> ops) {
this.ops = ops;
}
}
———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
package qau.edu.ren;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
public class Product {
private int id ;
private String name ;
private Date date ;
private Set<Op> op = new HashSet<Op>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Set<Op> getOp() {
return op;
}
public void setOp(Set<Op> op) {
this.op = op;
}
}
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
package qau.edu.ren;
import java.util.Date;
public class Op {
private int id ;
private double price ;
private Date date ;
private Order orders ;
private Product pros ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Order getOrders() {
return orders;
}
public void setOrders(Order orders) {
this.orders = orders;
}
public Product getPros() {
return pros;
}
public void setPros(Product pros) {
this.pros = pros;
}
}
———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
【关系映射文件】
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2014-11-12 20:48:07 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="qau.edu.ren.Op" table="OP">
<id name="id" type="int">
<column name="OP_ID" />
<generator class="native" />
</id>
<property name="price" type="double">
<column name="OP_PRICE" />
</property>
<property name="date" type="java.util.Date">
<column name="OP_DATE" />
</property>
<many-to-one name="orders" class="qau.edu.ren.Order" fetch="join">
<column name="O_ID" />
</many-to-one>
<many-to-one name="pros" class="qau.edu.ren.Product" fetch="join">
<column name="P_ID" />
</many-to-one>
</class>
</hibernate-mapping>
***********************************************************************************************************************************************************************************************************************************************
<?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="qau.edu.ren.Order" table="ORDER">
<id name="id" type="int">
<column name="O_ID" />
<generator class="native" />
</id>
<property name="date" type="java.util.Date">
<column name="O_DATE" />
</property>
<property name="name" type="java.lang.String">
<column name="O_NAME" />
</property>
<set name="ops" table="OP" inverse="true">
<key>
<column name="O_ID"/>
</key>
<one-to-many class="qau.edu.ren.Op" />
</set>
</class>
</hibernate-mapping>
***********************************************************************************************************************************************************************************************************************************************
<?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="qau.edu.ren.Product" table="PRODUCT">
<id name="id" type="int">
<column name="P_ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="P_NAME" />
</property>
<property name="date" type="java.util.Date">
<column name="P_DATE" />
</property>
<set name="op" table="OP" inverse="true">
<key>
<column name="P_ID" />
</key>
<one-to-many class="qau.edu.ren.Op" />
</set>
</class>
</hibernate-mapping>
************************************************************************************************************************************************************************************************************************************************
【配置文件】
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 配置数据表的生成方式 -->
<property name="hbm2ddl.auto">update</property>
<!-- 配置SQL输出的格式 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 配置关系映射文件的加载路径 -->
<mapping resource="qau/edu/ren/Product.hbm.xml" />
<mapping resource="qau/edu/ren/Op.hbm.xml" />
<mapping resource="qau/edu/ren/Order.hbm.xml" />
<!-- <mapping resource="qau/edu/jian/Person.hbm.xml"/> -->
</session-factory>
</hibernate-configuration>
************************************************************************************************************************************************************************************************************************************************
【测试类】
package qau.edu.ren;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
public static void main(String[] args) {
Session session = new Configuration().configure().buildSessionFactory()
.openSession();
Transaction tr = session.beginTransaction();
Order or = new Order();
or.setDate(new Date());
or.setName("AA");
Product pro = new Product();
pro.setDate(new Date());
pro.setName("BB");
Op op = new Op();
op.setDate(new Date());
op.setPrice(2232.22);
or.getOps().add(op);
pro.getOp().add(op);
session.save(pro);
session.save(or);
session.save(op);
tr.commit();
session.close();
}
}
【出现的错误:】
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate:
insert
into
PRODUCT
(P_NAME, P_DATE)
values
(?, ?)
Hibernate:
insert
into
ORDER (O_DATE
, O_NAME)
values
(?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [qau.edu.ren.Order]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2329)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2836)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:689)
at qau.edu.ren.Test.main(Test.java:39)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘ORDER (O_DATE, O_NAME) values (‘2014-11-12 22:30:17.0‘, ‘AA‘)‘ at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2648)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
... 16 more
标签:style http io ar os java sp for strong
原文地址:http://www.cnblogs.com/shiguangshuo/p/4093742.html