现在有两个对象 一个是模型对象 一个模型属性对象 他们的关系是1对多的关系 模型对象如下:
public class Model implements java.io.Serializable { private String modelId; //模型ID
private String modelName; //模型名称
private Set mapRelation = new HashSet(0); //映射关系
private Set modelProperties = new HashSet(0); //模型属性 public Model() {
} public Model(String modelId) {
this.modelId = modelId;
} public Model(String modelId, String modelName,
Set mapRelation, Set modelProperties) {
this.modelId = modelId;
this.modelName = modelName;
this.mapRelation = mapRelation;
this.modelProperties = modelProperties;
} public String getModelId() {
return modelId;
} public void setModelId(String modelId) {
this.modelId = modelId;
} public String getModelName() {
return modelName;
} public void setModelName(String modelName) {
this.modelName = modelName;
} public Set getMapRelation() {
return mapRelation;
} public void setMapRelation(Set mapRelation) {
this.mapRelation = mapRelation;
} public Set getModelProperties() {
return modelProperties;
} public void setModelProperties(Set modelProperties) {
this.modelProperties = modelProperties;
}
}模型属性:
public class ModelProperties implements java.io.Serializable { private String modelPropertId; //模型属性ID
private Model model; //模型
private String propertName; //属性名
private String propertType; //属性类型 public ModelProperties() {
} public ModelProperties(String modelPropertId) {
this.modelPropertId = modelPropertId;
} public ModelProperties(String modelPropertId, Model model, String propertName,
String propertType) {
this.modelPropertId = modelPropertId;
this.model = model;
this.propertName = propertName;
this.propertType = propertType;
} public String getModelPropertId() {
return modelPropertId;
} public void setModelPropertId(String modelPropertId) {
this.modelPropertId = modelPropertId;
} public Model getModel() {
return model;
} public void setModel(Model model) {
this.model = model;
} public String getPropertName() {
return propertName;
} public void setPropertName(String propertName) {
this.propertName = propertName;
} public String getPropertType() {
return propertType;
} public void setPropertType(String propertType) {
this.propertType = propertType;
}}他们的hbm.xml文件如下
<hibernate-mapping>
<class name="com.carnation.domain.Model" table="T_QO_MODEL" >
<id name="modelId" type="java.lang.String" unsaved-value="null">
<column name="MODELID" length="64" />
<generator class="uuid.hex"></generator>
</id>
<many-to-one name="rule" cascade="all" class="com.carnation.domain.Rule" fetch="select">
<column name="RULEID" length="64" />
</many-to-one>
<property name="modelName" type="java.lang.String" >
<column name="NAME" length="20" />
</property>
<set name="mapRelation" inverse="true">
<key>
<column name="MODELID" length="64" />
</key>
<one-to-many class="com.carnation.domain.MapRelation" />
</set>
<set name="modelProperties" inverse="false">
<key>
<column name="MODELID" length="64" />
</key>
<one-to-many class="com.carnation.domain.ModelProperties" />
</set>
</class>
</hibernate-mapping><hibernate-mapping>
<class name="com.carnation.domain.ModelProperties" table="T_QO_PROPERTIES" >
<id name="modelPropertId" type="java.lang.String" unsaved-value="null">
<column name="MODELPROID" length="64" />
<generator class="uuid.hex"></generator>
</id>
<many-to-one name="model" cascade="all" class="com.carnation.domain.Model" fetch="select">
<column name="MODELID" length="64" />
</many-to-one>
<property name="propertName" type="java.lang.String">
<column name="NAME" length="20" />
</property>
<property name="propertType" type="java.lang.String">
<column name="TYPE" length="20" />
</property>
<set name="propertSqlcolMap" inverse="true">
<key>
<column name="MODELPROID" length="64" />
</key>
<one-to-many class="com.carnation.domain.PropertSqlcolMap" />
</set>
</class>
</hibernate-mapping>
我想在保存模型属性对象的时候 设置模型属性对象表的模型ID为8a8080a637ab74950137ab7499c30002,其中模型表已经存在id为8a8080a637ab74950137ab7499c30002的记录 也就是我想保存模型属性对象的时候 不需要更新模型表 因为如果更新模型表的话 会导致模型表id为8a8080a637ab74950137ab7499c30002的记录别的字段为空 我应该怎么写才能到达效果或者怎么配置,请高手指教下面是我的测试代码public void testSave() {
ModelProperties mp = new ModelProperties();
mp.setPropertName("用户名");
Model model = new Model();
model.setModelId("8a8080a637ab74950137ab7499c30002");
//model = (Model)ruleDao.load("8a8080a637ab74950137ab7499c30002");
mp.setModel(model);
ruleDao.saveEntity(mp);
}
public class Model implements java.io.Serializable { private String modelId; //模型ID
private String modelName; //模型名称
private Set mapRelation = new HashSet(0); //映射关系
private Set modelProperties = new HashSet(0); //模型属性 public Model() {
} public Model(String modelId) {
this.modelId = modelId;
} public Model(String modelId, String modelName,
Set mapRelation, Set modelProperties) {
this.modelId = modelId;
this.modelName = modelName;
this.mapRelation = mapRelation;
this.modelProperties = modelProperties;
} public String getModelId() {
return modelId;
} public void setModelId(String modelId) {
this.modelId = modelId;
} public String getModelName() {
return modelName;
} public void setModelName(String modelName) {
this.modelName = modelName;
} public Set getMapRelation() {
return mapRelation;
} public void setMapRelation(Set mapRelation) {
this.mapRelation = mapRelation;
} public Set getModelProperties() {
return modelProperties;
} public void setModelProperties(Set modelProperties) {
this.modelProperties = modelProperties;
}
}模型属性:
public class ModelProperties implements java.io.Serializable { private String modelPropertId; //模型属性ID
private Model model; //模型
private String propertName; //属性名
private String propertType; //属性类型 public ModelProperties() {
} public ModelProperties(String modelPropertId) {
this.modelPropertId = modelPropertId;
} public ModelProperties(String modelPropertId, Model model, String propertName,
String propertType) {
this.modelPropertId = modelPropertId;
this.model = model;
this.propertName = propertName;
this.propertType = propertType;
} public String getModelPropertId() {
return modelPropertId;
} public void setModelPropertId(String modelPropertId) {
this.modelPropertId = modelPropertId;
} public Model getModel() {
return model;
} public void setModel(Model model) {
this.model = model;
} public String getPropertName() {
return propertName;
} public void setPropertName(String propertName) {
this.propertName = propertName;
} public String getPropertType() {
return propertType;
} public void setPropertType(String propertType) {
this.propertType = propertType;
}}他们的hbm.xml文件如下
<hibernate-mapping>
<class name="com.carnation.domain.Model" table="T_QO_MODEL" >
<id name="modelId" type="java.lang.String" unsaved-value="null">
<column name="MODELID" length="64" />
<generator class="uuid.hex"></generator>
</id>
<many-to-one name="rule" cascade="all" class="com.carnation.domain.Rule" fetch="select">
<column name="RULEID" length="64" />
</many-to-one>
<property name="modelName" type="java.lang.String" >
<column name="NAME" length="20" />
</property>
<set name="mapRelation" inverse="true">
<key>
<column name="MODELID" length="64" />
</key>
<one-to-many class="com.carnation.domain.MapRelation" />
</set>
<set name="modelProperties" inverse="false">
<key>
<column name="MODELID" length="64" />
</key>
<one-to-many class="com.carnation.domain.ModelProperties" />
</set>
</class>
</hibernate-mapping><hibernate-mapping>
<class name="com.carnation.domain.ModelProperties" table="T_QO_PROPERTIES" >
<id name="modelPropertId" type="java.lang.String" unsaved-value="null">
<column name="MODELPROID" length="64" />
<generator class="uuid.hex"></generator>
</id>
<many-to-one name="model" cascade="all" class="com.carnation.domain.Model" fetch="select">
<column name="MODELID" length="64" />
</many-to-one>
<property name="propertName" type="java.lang.String">
<column name="NAME" length="20" />
</property>
<property name="propertType" type="java.lang.String">
<column name="TYPE" length="20" />
</property>
<set name="propertSqlcolMap" inverse="true">
<key>
<column name="MODELPROID" length="64" />
</key>
<one-to-many class="com.carnation.domain.PropertSqlcolMap" />
</set>
</class>
</hibernate-mapping>
我想在保存模型属性对象的时候 设置模型属性对象表的模型ID为8a8080a637ab74950137ab7499c30002,其中模型表已经存在id为8a8080a637ab74950137ab7499c30002的记录 也就是我想保存模型属性对象的时候 不需要更新模型表 因为如果更新模型表的话 会导致模型表id为8a8080a637ab74950137ab7499c30002的记录别的字段为空 我应该怎么写才能到达效果或者怎么配置,请高手指教下面是我的测试代码public void testSave() {
ModelProperties mp = new ModelProperties();
mp.setPropertName("用户名");
Model model = new Model();
model.setModelId("8a8080a637ab74950137ab7499c30002");
//model = (Model)ruleDao.load("8a8080a637ab74950137ab7499c30002");
mp.setModel(model);
ruleDao.saveEntity(mp);
}
在 <many-to-one name="model" cascade="all" class="com.carnation.domain.Model" fetch="select">
<column name="MODELID" length="64" />
</many-to-one>
中配置了级联属性,级联 它只能在 一对多的关系中配置啊 ,要配置 也是在set标签中配置,它表示主表的修改 会级联修改 字表的记录 ,
cascade =“all”是不能写的。去掉就可以了。