使用eclipse+myeclipse+mysql
我有3张表如下
用户表:
create table User
(
id int auto_increment not null,
name varchar(50) not null,
pwd varchar(50) not null,
primary key (id)
)
type = InnoDB;
版面表:
create table Board
(
id int auto_increment not null,
create_by int not null,
parent_id int,
name varchar(50) not null,
remark varchar(255),
create_time datetime not null,
index (create_by),
foreign key (create_by) references user(id)
on delete cascade
on update cascade,
index(parent_id),
foreign key (parent_id) references Board(id)
on delete cascade
on update cascade,
primary key (id)
)
type = InnoDB;
文章表:
create table Article
(
id int auto_increment not null,
parent_id int,
board_id int not null,
article_type int not null,
title varchar(255) not null,
body text,
create_by int not null,
create_time datetime not null,
hits int not null,
bytes int,
last_update_by int not null,
last_update_time datetime not null,
index (parent_id),
foreign key (parent_id) references article (id)
on delete cascade
on update cascade,
index (board_id),
foreign key (board_id) references board (id)
on delete cascade
on update cascade,
index (create_by),
foreign key (create_by) references user (id)
on delete cascade
on update cascade,
index (last_update_by),
foreign key (last_update_by) references user (id)
on delete cascade
on update cascade,
primary key (id)
)
type = InnoDB;
通过以上SQL语句建了3张表,用的是mysql数据库
用eclipse+myeclipse开发
自动生成映射文件*.hbm.xml和POJO类
其中user.java为
public class User implements java.io.Serializable {
// Fields
private Integer id;
private String name;
private String pwd;
private Set articlesForLastUpdateBy = new HashSet(0);
private Set boards = new HashSet(0);
private Set articlesForCreateBy = new HashSet(0);
// Constructors
/** default constructor */
public User() {
}
/** minimal constructor */
public User(String name, String pwd) {
this.name = name;
this.pwd = pwd;
}
/** full constructor */
public User(String name, String pwd, Set articlesForLastUpdateBy,
Set boards, Set articlesForCreateBy) {
this.name = name;
this.pwd = pwd;
this.articlesForLastUpdateBy = articlesForLastUpdateBy;
this.boards = boards;
this.articlesForCreateBy = articlesForCreateBy;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return this.pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Set getArticlesForLastUpdateBy() {
return this.articlesForLastUpdateBy;
}
public void setArticlesForLastUpdateBy(Set articlesForLastUpdateBy) {
this.articlesForLastUpdateBy = articlesForLastUpdateBy;
}
public Set getBoards() {
return this.boards;
}
public void setBoards(Set boards) {
this.boards = boards;
}
public Set getArticlesForCreateBy() {
return this.articlesForCreateBy;
}
public void setArticlesForCreateBy(Set articlesForCreateBy) {
this.articlesForCreateBy = articlesForCreateBy;
}
}
其中user.hbm.xml文件内容为
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="cn.tiger.config.User" table="user" catalog="test4">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="50" not-null="true" />
</property>
<property name="pwd" type="java.lang.String">
<column name="pwd" length="50" not-null="true" />
</property>
<set name="articlesForLastUpdateBy" inverse="true">
<key>
<column name="last_update_by" not-null="true" />
</key>
<one-to-many class="cn.tiger.config.Article" />
</set>
<set name="boards" inverse="true">
<key>
<column name="create_by" not-null="true" />
</key>
<one-to-many class="cn.tiger.config.Board" />
</set>
<set name="articlesForCreateBy" inverse="true">
<key>
<column name="create_by" not-null="true" />
</key>
<one-to-many class="cn.tiger.config.Article" />
</set>
</class>
</hibernate-mapping>
然后写了一个测试类TestCRUD.java
public class TestCRUD extends TestCase {
Session session = null;
protected void setUp() throws Exception {
Configuration cfg = new Configuration().configure();
session = cfg.buildSessionFactory().openSession();
}
protected void tearDown() throws Exception {
session.close();
}
public void testCRUD() throws HibernateException{
Transaction tra = null;
tra = session.beginTransaction();
User user = new User();
user.setName("someone");
user.setPwd("guessme");
session.save(user);
session.flush();
User user2 = (User) session.load(User.class, user.getId());
assertEquals("someone",user2.getName());
assertEquals("guessme",user2.getPwd());
user2.setPwd("guessAgain");
session.saveOrUpdate(user2);
session.flush();
user = (User) session.load(user.getClass(), user.getId());
assertEquals("guessAgain",user.getPwd());
session.delete(user);
session.flush();
tra.commit();
}
}
但却出现这样的异常:org.hibernate.PropertyNotFoundException: Could not find a getter for articlesForLastUpdateBy in class cn.tiger.config.User
at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:282)
at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:275)
at org.hibernate.mapping.Property.getGetter(Property.java:260)
at org.hibernate.tuple.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:255)
at org.hibernate.tuple.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:121)
at org.hibernate.tuple.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:55)
at org.hibernate.tuple.TuplizerLookup.create(TuplizerLookup.java:64)
at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:257)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:412)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:108)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:216)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
at cn.tiger.test.TestCRUD.setUp(TestCRUD.java:16)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
大家帮帮我哦,谢谢了哦
[此贴子已经被作者于2007-8-28 10:47:46编辑过]