hibernate4 注解配置失误导致生成的表关联关系不对
程序员文章站
2022-05-19 23:26:30
...
最近做ibm 风险管理系统,配置hibernate 注解时候看了几遍都是正确的,但是生成的表总是无关联关系,即便是有关联的也不对。testng时候也不报错。。。
错误原因不说了,直接上代码。
package com.ibm.banking.irmp.dataimport; import java.io.Serializable; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Transient; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.NamedQueries; import org.hibernate.annotations.NamedQuery; import com.ibm.banking.framework.dto.BaseVO; @Entity @Table(name = "RI_NT_REGULAR_DEF") @NamedQueries({ @NamedQuery(name = "RegularTable.getAll", query = "select regularData from RegularData regularData order by regularData.id", readOnly = true, cacheable = true, cacheRegion = "RefBean") }) @Cache(usage = CacheConcurrencyStrategy.READ_ONLY, region = "RefBean") public class RegularData implements BaseVO, Serializable { /** * */ private static final long serialVersionUID = 5395568085328896385L; /** * @author xushigang */ private Long id; private String tableName; private List<RegularColMapping> mapping; public RegularData() { } public RegularData(Long tableId) { super(); this.id = tableId; } @Id @GeneratedValue public Long getId() { return id; } public void setId(Long tableId) { this.id = tableId; } public String getTableName() { return tableName; } public void setTableName(String tableName) { this.tableName = tableName; } @OneToMany(cascade = CascadeType.ALL, mappedBy="regularData") public List<RegularColMapping> getMapping() { return mapping; } public void setMapping(List<RegularColMapping> mapping) { this.mapping = mapping; } }
package com.ibm.banking.irmp.dataimport; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import com.ibm.banking.framework.dto.BaseVO; @Entity @Table(name = "RI_NT_REGULAR_COL") @Cache(usage = CacheConcurrencyStrategy.READ_ONLY, region = "RefBean") public class RegularColMapping implements BaseVO, Serializable { /** * @author xushigang */ private static final long serialVersionUID = 1L; private Long id; private String regularName; private String description; private String regularType; private String columnName; private String sql; private String tag; private RegularData regularData; public RegularColMapping() {} public RegularColMapping(String columnName) { super(); this.columnName = columnName; } @Id @GeneratedValue public Long getId() { return id; } public void setId(Long regularId) { this.id = regularId; } public String getRegularName() { return regularName; } public void setRegularName(String regularName) { this.regularName = regularName; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getRegularType() { return regularType; } public void setRegularType(String regularType) { this.regularType = regularType; } public String getColumnName() { return columnName; } public void setColumnName(String columnName) { this.columnName = columnName; } public String getSql() { return sql; } public void setSql(String sql) { this.sql = sql; } public String getTag() { return tag; } public void setTag(String tag) { this.tag = tag; } @ManyToOne @JoinColumn(name="TABLE_ID") public RegularData getRegularData() { return regularData; } public void setRegularData(RegularData regularData) { this.regularData = regularData; } }
以上是正确的配置,我原始的配置是 将@Id 放在了
private Long id;
上面,但这也不是主要原因,我实现的baseVo里面有序列id,我没有加 @transient,但是也没报错,很是怪异。