本人小白,表示视频一看就觉得会了,做起来老是出错,严重影响学习进度,为纠正错误我反复又把视频看了很多遍,百度查了说是跟主键生成方式有关,也按自己能理解的去做了,现在真是不知道错在哪儿了,下面贴出代码,请大虾高手前辈帮忙指点指点,感激不尽!!!oralce数据库建表语句:
CREATE TABLE GRADE (CID NUMBER(20) NOT NULL CONSTRAINT PK_CLA PRIMARY KEY,
CNAME VARCHAR2(50),
CDESC VARCHAR2(200));
CREATE TABLE STUDENT (SID NUMBER(20) NOT NULL CONSTRAINT PK_STU PRIMARY KEY,
SNAME VARCHAR2(50),
SAGE NUMBER(2),
SSEX VARCHAR2(2),
SPHONENO VARCHAR2(15),
CID NUMBER(20) NOT NULL);
ALTER TABLE STUDENT ADD CONSTRAINT FK_STU FOREIGN KEY (CID) REFERENCES GRADE(CID);hibernate运行的控制台信息:2017-5-11 19:46:17 org.hibernate.tool.hbm2ddl.TableMetadata <init>INFO: HHH000261: Table found: MKADMIN.GRADE2017-5-11 19:46:17 org.hibernate.tool.hbm2ddl.TableMetadata <init>INFO: HHH000037: Columns: [cdesc, cid, cname]2017-5-11 19:46:17 org.hibernate.tool.hbm2ddl.TableMetadata <init>INFO: HHH000108: Foreign keys: []2017-5-11 19:46:17 org.hibernate.tool.hbm2ddl.TableMetadata <init>INFO: HHH000126: Indexes: [pk_cla]2017-5-11 19:46:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>INFO: HHH000261: Table found: MKADMIN.STUDENT2017-5-11 19:46:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>INFO: HHH000037: Columns: [sid, sage, sphoneno, ssex, sname, cid]2017-5-11 19:46:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>INFO: HHH000108: Foreign keys: [fk_stu]2017-5-11 19:46:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>INFO: HHH000126: Indexes: [pk_stu]2017-5-11 19:46:18 org.hibernate.tool.hbm2ddl.SchemaUpdate executeINFO: HHH000232: Schema update completeHibernate: insert into GRADE (CNAME, CDESC, CID) values (?, ?, ?)Hibernate: update Student set CID=? where SID=?这里为什么GRADE表是插入,学生表却是更新呢?是不是错在这?JUNIT4 的错误跟踪信息:org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:81) at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:73) at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:59) at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1256) at org.hibernate.persister.collection.OneToManyPersister.recreate(OneToManyPersister.java:183) at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:58) at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:292) at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234) at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404) at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175) at HbmTest.destory(HbmTest.java:31) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:36) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)配置文件hibernate.cfg.xml:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="javax.persistence.validation.mode">none</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">MKADMIN</property>
<property name="hibernate.connection.password">MK123456</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
<mapping resource="Grade.hbm.xml"/>
<mapping resource="Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>映射文件:Grade.hbm.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- dynamic-update 列动态更新,那列变化更新那个 -->
<class name="po.Grade" table="GRADE" dynamic-update="true">
<id name="cId" type="java.lang.Integer">
<column name="CID" length="20"/>
<generator class="assigned"/>
</id>
<property name="cName" type="java.lang.String">
<column name="CNAME" length="50"/>
</property>
<property name="cDesc" type="java.lang.String">
<column name="CDESC" length="200"/>
</property>
<set name="students" table="student">
<key column="CID"/>
<one-to-many class="po.Student"/>
</set>
</class>
</hibernate-mapping>映射文件:Student.hbm.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- dynamic-update 字段动态更新,那个变化更新那个 -->
<class name="po.Student" table="Student" dynamic-update="true">
<id name="sId" type="java.lang.Integer">
<column name="SID" length="20"/>
<generator class="assigned"/>
</id>
<property name="sName" type="java.lang.String">
<column name="SNAME" length="50"/>
</property>
<property name="sPhoneNo" type="java.lang.String">
<column name="SPHONENO" length="15"/>
</property>
<property name="sSex" type="java.lang.String">
<column name="SSEX" length="2"/>
</property>
<property name="sAge" type="java.lang.Integer">
<column name="SAGE" length="2"/>
</property>
</class>
</hibernate-mapping>测试类代码:因为实体类主要是属性和构造方法,就不贴了import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import po.Grade;
import po.Student;
public class HbmTest {
private SessionFactory sf=null;
private Session session=null;
private Transaction tran=null;
@Before
public void init(){
Configuration cfg=new Configuration().configure();
ServiceRegistry sr=new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
sf=cfg.buildSessionFactory(sr);
session=sf.getCurrentSession();
tran=session.beginTransaction();
}
@After
public void destory(){
tran.commit();
sf.close();
}
@Test
public void test(){
Grade c1=new Grade(1,"java1班","java软件开发一班");
Student s1=new Student(1,"黄三","07913356566","男",15);
Student s2=new Student(2,"王五","07913300000","女",18);
Set<Student>students=new HashSet<Student>();
students.add(s1);
students.add(s2);
c1.setStudents(students);
session.save(c1);
}
}
添加回答
举报
0/150
提交
取消