Hibernate 多对一查询,以及一对多关联删除

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u010363836/article/details/52083047

类别、标签(一个类别,有多个标签)

一对多关联删除(在一的这边进行如下配置就可以了)

在删除“一”的这一方的数据时,“多”的这一方的相关数据也会被删除;

@OneToMany(mappedBy="articleCategory",fetch=FetchType.EAGER,cascade=CascadeType.REMOVE)
private Set<ArticleTags> articleTags;  //标签

类别完整的实体类代码:

package com.wamei.entity;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Set;

/**
 * APP版本表
 * @author chain
 * 文章类别
 */
@Entity
@Table(name="T_ARTICLE_CATEGORY")
public class ArticleCategory implements Serializable {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(generator = "paymentableGenerator")
	@GenericGenerator(name = "paymentableGenerator", strategy = "uuid")
	@Column(name = "ID")
	private String id;

	@Column(name = "NAME")
	private String name;

	@OneToMany(mappedBy="articleCategory",fetch=FetchType.EAGER,cascade=CascadeType.REMOVE)
	private Set<ArticleTags> articleTags;	//标签

	public String getId() {
		return id;
	}


	@Column(name = "ORDER_INDEX")
	private int orderIndex;

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public int getOrderIndex() {
		return orderIndex;
	}

	public void setOrderIndex(int orderIndex) {
		this.orderIndex = orderIndex;
	}

	public Set<ArticleTags> getArticleTags() {
		return articleTags;
	}

	public void setArticleTags(Set<ArticleTags> articleTags) {
		this.articleTags = articleTags;
	}
}



多对一的查询(在查询“多” 的这一方时,把“一”的这一方的相关数据也查询出来)

在“多”的一方的实体类进行如下配置

@ManyToOne
@JoinColumn(name = "CATEGOTY_ID")
private ArticleCategory articleCategory;

获取“一”这一方的相关字段的属性设置如下

@Transient
public String getCategoryName(){
   String str = "";
   try {
      str = null==this.articleCategory?"无":articleCategory.getName();
   } catch (Exception e) {
      str = "无";
   }
   return str;
}

注意:在添加多的一方数据时,前端页面的属性名称写法如下

articleCategory是“多”的一方的属性对象

所以类别的ID的属性的写法应该是:articleCategory.id (这里的id对应的是类别的属性)

					<div class="field-box">
						<label>类别:</label>
						<div class="ui-select">
							<select style="height: 32px;" id="categoryId" name="articleCategory.id">
								<option value="">--请选择--</option>
								<c:forEach items="${cataList}" var="item" varStatus="status">
									<option value="${item.id}">${item.name}</option>
								</c:forEach>
							</select>
						</div>
					</div>



标签实体类:

package com.wamei.entity;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;

/**
 * APP版本表
 * @author chain
 * 文章标签
 */
@Entity
@Table(name="T_ARTICLE_TAGS")
public class ArticleTags implements Serializable {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(generator = "paymentableGenerator")
	@GenericGenerator(name = "paymentableGenerator", strategy = "uuid")
	@Column(name = "ID")
	private String id;

	@Column(name = "NAME")
	private String name;

	@Column(name = "ORDER_INDEX")
	private int orderIndex;

	@ManyToOne
	@JoinColumn(name = "CATEGOTY_ID")
	private ArticleCategory articleCategory;


	public String getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getOrderIndex() {
		return orderIndex;
	}

	public void setOrderIndex(int orderIndex) {
		this.orderIndex = orderIndex;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public ArticleCategory getArticleCategory() {
		return articleCategory;
	}

	public void setArticleCategory(ArticleCategory articleCategory) {
		this.articleCategory = articleCategory;
	}

	@Transient
	public String getCategoryName(){
		String str = "";
		try {
			str = null==this.articleCategory?"无":articleCategory.getName();
		} catch (Exception e) {
			str = "无";
		}
		return str;
	}

}



展开阅读全文

没有更多推荐了,返回首页