Hibernate一对多,自动生成表的问题。高手进来看下。

shusheng1997 2012-03-09 11:11:53
在搞一对多的映射关系的时候,自动生成表,只能生成一方的表,提示多方的表创建失败。
一方表配置:

<?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">

<hibernate-mapping package="com.zbcl.bean">
<class name="Images" table="images">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"></property>
<property name="src" column="src"></property>
<set name="figure" cascade="all" inverse="true" >
<key column="id" />
<one-to-many class="Figure"/>
</set>
</class>
</hibernate-mapping>
多方表配置:

<?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">

<hibernate-mapping package="com.zbcl.bean">
<class name="Figure" table="figure">
<id name="figureId" column="figureId">
<generator class="native" />
</id>
<property name="name" column="name"></property>
<property name="src" column="src"></property>
<property name="place" column="place"></property>
<property name="order" column="order"></property>
<many-to-one name="images" column="id" class="Images" not-null="true" cascade="all">
</many-to-one>
</class>
</hibernate-mapping>
JavaBean:

package com.zbcl.bean;

public class Figure {
private int figureId;
private String name;
private String src;
private String place;
private String order;
private Images images;
public Figure(){}
public Images getImages() {
return images;
}
public void setImages(Images images) {
this.images = images;
}
public int getFigureId() {
return figureId;
}
public void setFigureId(int figureId) {
this.figureId = figureId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
}

package com.zbcl.bean;

import java.util.HashSet;
import java.util.Set;

public class Images {
private int id;
private String name;
private String src;
private Set<Figure> figure=new HashSet<Figure>();

public Images(){}
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 String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public Set<Figure> getFigure() {
return figure;
}
public void setFigure(Set<Figure> figure) {
this.figure = figure;
}
}

测试代码:

package com.zbcl.test;

import java.util.HashSet;
import java.util.Set;

import junit.framework.TestCase;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zbcl.action.ImgManageAction;
import com.zbcl.bean.Figure;
import com.zbcl.bean.Images;


public class oneToMany extends TestCase{
public void testAdd(){
Set<Figure> set=new HashSet<Figure>();
Images images=new Images();
images.setName("ditie");
images.setSrc("12345");
Figure figure=new Figure();
figure.setName("xiaotu");
figure.setSrc("74108520");
figure.setOrder("sdasdasd");
figure.setPlace("sdsadasd");
figure.setImages(images);
set.add(figure);
images.getFigure().add(figure);
Figure figure1=new Figure();
figure1.setName("xiaotu");
figure1.setSrc("74108520");
figure1.setOrder("sdasdasd");
figure1.setPlace("sdsadasd");
figure1.setImages(images);
set.add(figure1);
images.setFigure(set);

try{
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
ImgManageAction action=(ImgManageAction)context.getBean("imgManageAction");
action.add(images);
}catch (Exception e) {
e.printStackTrace();
}
}
}
...全文
298 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

23,405

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧