Java 使用@IndexColumn会导致seq_num为0
我想使用@IndexColumn设置用户输入的某些数据的序号。我使用的是Spring2.5.6、JBoss5.1(JPA1.0) 为了我的家长班Java 使用@IndexColumn会导致seq_num为0,java,hibernate,jpa,Java,Hibernate,Jpa,我想使用@IndexColumn设置用户输入的某些数据的序号。我使用的是Spring2.5.6、JBoss5.1(JPA1.0) 为了我的家长班 @Entity @Table(name="material") public class Material implements Serializable { . . /** * List of material attributes associated with the given material */ @On
@Entity
@Table(name="material")
public class Material implements Serializable {
.
.
/**
* List of material attributes associated with the given material
*/
@OneToMany(mappedBy = "material", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@IndexColumn(name="seq_number", base=0, nullable = false)
private List<MaterialAttribute> materialAttributes;
public void addMaterialAttribute(List<MaterialAttribute> attribs)
{
if(CollectionUtils.isNotEmpty(attribs))
{
for(MaterialAttribute attrib : attribs)
{
attrib.setMaterial(this);
}
this.setMaterialAttributes(attribs);
}
}
}
服务类
public void save(MaterialCommand pCmd)
{
Material material = new Material(pCmd.getName());
//convert from command object to entity object
List<MaterialAttribute> attribs = new ArrayList<MaterialAttribute>();
if(CollectionUtils.isNotEmpty(pCmd.getAttribs()))
{
Iterator<MaterialAttributeCommand> iter = pCmd.getAttribs().iterator();
while(iter.hasNext())
{
MaterialAttributeCommand attribCmd = (MaterialAttributeCommand) iter.next();
MaterialAttribute attrib = new MaterialAttribute();
attrib.setDisplayName(attribCmd.getDisplayName());
attrib.setValidationType(attribCmd.getValidationType());
attribs.add(attrib);
}
}
material.addMaterialAttribute(attribs);
this.getMaterialDAO().saveMaterial(material);
}
公共作废保存(物料命令和pCmd)
{
物料物料=新物料(pCmd.getName());
//从命令对象转换为实体对象
List attribs=new ArrayList();
if(CollectionUtils.isNotEmpty(pCmd.getAttribs()))
{
迭代器iter=pCmd.getAttribs().Iterator();
while(iter.hasNext())
{
MaterialAttributeCommand attribCmd=(MaterialAttributeCommand)iter.next();
MaterialAttribute属性b=新MaterialAttribute();
attrib.setDisplayName(attribCmd.getDisplayName());
attrib.setValidationType(attribCmd.getValidationType());
attribs.add(attrib);
}
}
材料。添加材料属性(属性);
此.getMaterialDAO().saveMaterial(物料);
}
我正在将条目输入数据库,但对于集合中的每个项目,序号始终为零
我必须假设这是我保存数据的方式,但我只是看不到它
我已通过以下操作解决了问题(删除了mappedBy):
@Entity
@Table(name="material")
public class Material implements Serializable {
/**
*
*/
private static final long serialVersionUID = 5083931681636496023L;
@Column(name="name", length=50, nullable=false)
private String mName;
/**
* List of material attributes associated with the given material
*/
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@IndexColumn(name="seq_number", base=0)
@JoinColumn(name="material_id",nullable=false)
private List<MaterialAttribute> materialAttributes;
@Entity
@Table(name="material_attribute")
public class MaterialAttribute implements Serializable
{
/**
*
*/
private static final long serialVersionUID = -196083650806575093L;
/**
* identifies the material that these attributes are associated with
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "material_id", insertable=false, updatable=false, nullable = true, unique = false)
private Material material;
@Column(name = "seq_number", insertable=false, updatable=false)
private int seqNumber;
@实体
@表(name=“material”)
公共类材质实现可序列化{
/**
*
*/
私有静态最终长serialVersionUID=5083931681636496023L;
@列(name=“name”,长度=50,可空=false)
私有字符串mName;
/**
*与给定材质关联的材质属性列表
*/
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@索引列(name=“seq_number”,基数=0)
@JoinColumn(name=“material\u id”,null=false)
私人物品清单;
@实体
@表(name=“物料属性”)
公共类MaterialAttribute实现可序列化
{
/**
*
*/
私有静态最终长serialVersionUID=-196083650806575093L;
/**
*标识与这些属性关联的材质
*/
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“material\u id”,insertable=false,updateable=false,nullable=true,unique=false)
私人材料;
@列(name=“seq_number”,insertable=false,updateable=false)
私有整数序列号;
用Hibernate映射一个双向的索引列表有点棘手,但在文档的一节中有介绍(粗体是我的):
一种双向关联,其中
end是一个索引集合(即。
表示为@OrderColumn
,或表示为
映射
)需要特殊的
对价。如果在
关联类显式映射
索引值,使用mappedBy
是允许的:
@Entity
public class Parent {
@OneToMany(mappedBy="parent")
@OrderColumn(name="order")
private List<Child> children;
...
}
@Entity
public class Child {
...
//the index column is mapped as a property in the associated entity
@Column(name="order")
private int order;
@ManyToOne
@JoinColumn(name="parent_id", nullable=false)
private Parent parent;
...
}
请注意,在此映射中,
集合值结束时
协会负责
更新外键
实际上,第二种映射正是如何将双向一对多映射为拥有方的一对多。虽然这是可能的,但您需要注意,这种映射将产生未经优化的SQL,如关于[一对多]关系的一节所述:
要将双向一对多映射,
以一对多的一面为基础
拥有方,您必须删除
mappedBy
元素,并将many设置为
一个@JoinColumn
作为可插入的
和
updateable
到false
此解决方案是
未优化,将产生一些
其他更新声明。
总之,如果将索引列映射为目标实体的属性不是一个问题,这将是我的建议(即第一个映射)
工具书类
- Hibernate注释3.4参考指南
- [一对多]
@Entity
public class Parent {
@OneToMany(mappedBy="parent")
@OrderColumn(name="order")
private List<Child> children;
...
}
@Entity
public class Child {
...
//the index column is mapped as a property in the associated entity
@Column(name="order")
private int order;
@ManyToOne
@JoinColumn(name="parent_id", nullable=false)
private Parent parent;
...
}
@Entity
public class Parent {
@OneToMany
@OrderColumn(name="order")
@JoinColumn(name="parent_id", nullable=false)
private List<Child> children;
...
}
@Entity
public class Child {
...
@ManyToOne
@JoinColumn(name="parent_id", insertable=false, updatable=false, nullable=false)
private Parent parent;
...
}