hibernate4注解配置,纠结的一对多双向关联

阅读更多

目前负责中行风险管理系统的数据核对加工模块,业务以及逻辑都已经梳理好了,但是实施的时候遇到了点问题,hibernate注解配置问题。闲话不多说直接上正确的配置:

 

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.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

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 mapping;

	public RegularData() {
	}

	public RegularData(Long tableId) {
		super();
		this.id = tableId;
	}

	@Id
	@GeneratedValue
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}


	@OneToMany(cascade=CascadeType.ALL, orphanRemoval=true)
	@JoinColumn(name="table_id")
	public List getMapping() {
		return mapping;
	}

	public void setMapping(List mapping) {
		this.mapping = mapping;
	}
}

 

package com.ibm.banking.irmp.dataimport;

import java.io.Serializable;

import javax.persistence.CascadeType;
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 id) {
		this.id = id;
	}
	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(cascade=CascadeType.ALL)
	@JoinColumn(name="table_id")
	public RegularData getRegularData() {
		return regularData;
	}
	public void setRegularData(RegularData regularData) {
		this.regularData = regularData;
	}
	
}
 
@Test
	public void testRegular(){
		//List o = session.createQuery(sql.toString()).list();
		//StringBuffer sb = new StringBuffer();
		//sb.append(Restrictions.in("tableId", new String[]{"1","2","3"}));
		//sb.append(Restrictions.ilike("mapping.regularName","aa", MatchMode.ANYWHERE));
		//System.out.println(sb.toString());
		//System.out.println(o.get(0)[0]);
		//System.out.println(o.get(0)[1]);
		//session.enableFilter("a");
		Session session= dao.getSession();
		//List rd = session.createQuery("from RegularData rd").list();
		List mapping = new ArrayList();
		RegularData tf = new RegularData();
		tf.setTableName("RI_NT_SYS_FUNC");
		RegularColMapping rc = new RegularColMapping();
		rc.setColumnName("column_name");
		rc.setSql("sql");
		//rc.setRegularData(tf);
		mapping.add(rc);
		RegularColMapping rc1 = new RegularColMapping();
		rc1.setColumnName("column_name");
		rc1.setSql("sql");
		//rc1.setRegularData(tf);
		mapping.add(rc1);
		tf.setMapping(mapping);
		session.save(tf);
		
	}

 

以上配置是正确的,多的一端会自动生成table_id 并且会自动插入数值。

 

按照常规的配置:

 

@OneToMany(cascade=CascadeType.ALL, orphanRemoval=true,mappedBy="regularData")
	
	public List getMapping() {
		return mapping;
	}

 这样的话table_id就会插入 null 需要手工 关联!!!

 

测试结果如下:

Hibernate: 
    select
        hibernate_sequence.nextval 
    from
        dual
Hibernate: 
    select
        hibernate_sequence.nextval 
    from
        dual
Hibernate: 
    select
        hibernate_sequence.nextval 
    from
        dual
Hibernate: 
    insert 
    into
        ri_nt_regular_def
        (table_name, id) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        ri_nt_regular_col
        (column_name, description, table_id, regular_name, regular_type, sql, tag, id) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        ri_nt_regular_col
        (column_name, description, table_id, regular_name, regular_type, sql, tag, id) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?)

 

 

你可能感兴趣的:(hibernate4注解配置,纠结的一对多双向关联)