package many;
public class Publication {
private String id;
private String bookName = null;
private String stateDate = null;
private Author author = null;
/**
* Method setId
*
*
* @param id
*
*/
public void setId(String id) {
// TODO: Add your code here
this.id = id;
} /**
* Method getId
*
*
* @return
*
*/
public String getId() {
// TODO: Add your code here
return this.id;
} /**
* Method getBookName
*
*
* @return
*
*/
public String getBookName() {
// TODO: Add your code here
return this.bookName;
} /**
* Method setBookName
*
*
* @param bookName
*
*/
public void setBookName(String bookName) {
// TODO: Add your code here
this.bookName = bookName;
} /**
* Method setDate
*
*
* @param date
*
*/
public void setStateDate(String stateDate) {
// TODO: Add your code here
this.stateDate = stateDate;
} /**
* Method getDate
*
*
* @return
*
*/
public String getStateDate() {
// TODO: Add your code here
return this.stateDate;
}
public void setAuthor( Author author )
{
this.author = author;
}
public Author getAuthor()
{
return this.author;
}
}
public class Publication {
private String id;
private String bookName = null;
private String stateDate = null;
private Author author = null;
/**
* Method setId
*
*
* @param id
*
*/
public void setId(String id) {
// TODO: Add your code here
this.id = id;
} /**
* Method getId
*
*
* @return
*
*/
public String getId() {
// TODO: Add your code here
return this.id;
} /**
* Method getBookName
*
*
* @return
*
*/
public String getBookName() {
// TODO: Add your code here
return this.bookName;
} /**
* Method setBookName
*
*
* @param bookName
*
*/
public void setBookName(String bookName) {
// TODO: Add your code here
this.bookName = bookName;
} /**
* Method setDate
*
*
* @param date
*
*/
public void setStateDate(String stateDate) {
// TODO: Add your code here
this.stateDate = stateDate;
} /**
* Method getDate
*
*
* @return
*
*/
public String getStateDate() {
// TODO: Add your code here
return this.stateDate;
}
public void setAuthor( Author author )
{
this.author = author;
}
public Author getAuthor()
{
return this.author;
}
}
解决方案 »
- java小菜鸟的困惑:项目中导入过多的jar包,会不会影响效率!
- 寻找高手指路,在线等!
- 大家帮忙看看这个我应该怎么转?
- spring和hibernate整合出的问题。。
- 从数据表中读取出数据显示到Treeview中 请各位大侠帮帮菜鸟
- 高分求助!EJB编译问题!古怪!在线急等!!
- 惨啊,要交毕业论文了,高手求救啊!!!关于Java与COM通信
- 使用notepad++编写的java程序里面有中文字符,编译通不过怎么解决?
- SSH2+easyui的数据库交互问题
- hibernate多对多关联如何如何删除中间表的关系,而不删除两张表中的数据
- 连JDBC时出的奇怪问题,大虾们帮一把阿
- 请教高手Eclipse3.0.1平台的配置问题
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;public class Example{
private static SessionFactory _sessions = null;
private static Properties pops = new Properties();
static{
try {
Configuration cfg = new Configuration();
cfg.addClass(Author.class);
cfg.addClass(Publication.class);
SchemaExport dbExport = new SchemaExport(cfg);
dbExport.create(true, true);
_sessions = cfg.buildSessionFactory();
} catch (MappingException e) {
e.printStackTrace();
} catch (HibernateException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws HibernateException {
Publication publication1 = new Publication();
publication1.setId("00001");
publication1.setBookName("AAA");
publication1.setStateDate("20031224");
Publication publication2 = new Publication();
publication2.setId("00002");
publication2.setBookName("BBB");
publication2.setStateDate("20031225");
Author author = new Author(); author.setId("Z0001");
author.setName("smallduzi");
author.getPublications().add(publication1);
author.getPublications().add(publication2);
//publication1.setAuthor(author);
//publication2.setAuthor(author); Session session = _sessions.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
session.save(author);
session.flush();
tx.commit();
System.out.println("over");
}catch(HibernateException he){
if(tx != null) tx.rollback();
throw he;
}
finally{
session.close();
}
}
}
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="many.Author" table="author" dynamic-update="true" dynamic-insert="true">
<id name="id" column="id">
<!--uuid.hex-->
<generator class="assigned"/>
</id>
<property name="name" column="Name" />
<set name="publications" lazy="false" inverse="true" cascade="all">
<key column="author_id"/>
<one-to-many class="many.Publication" />
</set>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="many.Publication" table="Publication" dynamic-update="true" dynamic-insert="true">
<id name="id" column="Id">
<!--
<generator class="sequence">
<param name="sequence">Reader_Id_Seq</param>
</generator>
-->
<generator class="assigned"/>
</id>
<property name="bookName" column="BookName" />
<property name="stateDate" column="stateDate" />
<many-to-one name="author" cascade="all" outer-join="auto" update="true" insert="true" column="author_id" />
</class>
</hibernate-mapping>
update Publication set BookName=?, stateDate=?, author_id=? where Id=?而不是insert呢?问题就可能在这里。请帮忙看看。create table Publication (
Id VARCHAR2(255) not null,
BookName VARCHAR2(255),
stateDate VARCHAR2(255),
author_id VARCHAR2(255),
primary key (Id)
)
create table author (
id VARCHAR2(255) not null,
Name VARCHAR2(255),
primary key (id)
)
alter table Publication add constraint FK23254A0C57F3E04F foreign key (author_id) references author
Hibernate: insert into author (Name, id) values (?, ?)
Hibernate: update Publication set BookName=?, stateDate=?, author_id=? where Id=?
Exception in thread "main" net.sf.hibernate.HibernateException: SQL update or deletion failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:642)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:611)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:31)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2100)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2062)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2005)
at many.Example.main(Example.java:66)
2.你的Author(parent)的inverse=false,cascade="all"表明由parent控制关联,并且级联save,update,delete child.
3.save parent时,hibernate发现parent拥有的child的主键都不为空,认为children都是已持久化的po,于是调用update 的sql更新所有的child状态.
4.你的数据库中没有存储期待被更新的po的数据,发生异常.