码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate createQuery调用joincolumn

时间:2015-03-10 23:05:14      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

1. Beans

a. Version Bean

package locationService.beans;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

import org.hibernate.annotations.GenericGenerator;

import locationService.beans.Entities;
import locationService.beans.Attribute;
import locationService.beans.AttributeTag;
import locationService.beans.EntityToEntity;
import locationService.beans.Tag;

@Entity
@Table(name = "version")
public class Version {
  private int versionId;
  private boolean complete;
  private Timestamp editDate;
  private String author;
  private String description;
  private int preVersionId;
  private List<Entities> entities = new ArrayList<Entities>();
  private List<Attribute> attribute = new ArrayList<Attribute>();
  private List<AttributeTag> attributeTag = new ArrayList<AttributeTag>();
  private List<EntityToEntity> entityToEntity = new ArrayList<EntityToEntity>();
  private List<Tag> tag = new ArrayList<Tag>();
  

  @Id
  @GeneratedValue(generator = "increment")
  @GenericGenerator(name = "increment", strategy = "increment")
  @Column(name="version_id")
  public int getVersionId() {
	return versionId;
  }
  public void setVersionId(int versionId) {
	this.versionId = versionId;
  }
  
  @Column(name="complete")
  public boolean getComplete() {
	return complete;
  }
  public void setComplete(boolean complete) {
	this.complete = complete;
  }

  @Column(name="edit_date")
  public Timestamp getEditDate() {
	return editDate;
  }
  public void setEditDate(Timestamp editDate) {
	this.editDate = editDate;
  }

  @Column(name="author")
  public String getAuthor() {
	return author;
  }
  public void setAuthor(String author) {
	this.author = author;
  }

  @Column(name="description")
  public String getDescription() {
	return description;
  }
  public void setDescription(String description) {
	this.description = description;
  }

  @Column(name="pre_version_id")
  public int getPreVersionId() {
	return preVersionId;
  }
  public void setPreVersionId(int preVersionId) {
	this.preVersionId = preVersionId;
  }
  

  @OneToMany(cascade = CascadeType.ALL)
  @JoinColumn(name = "version_id", nullable = false)
  public List<Entities> getEntities() {
	return entities;
  }
  public void setEntities(List<Entities> entities) {
	this.entities = entities;
  }
  
  @Override
  public String toString() {
    return "versionId is " + versionId + ", preVersionId is " + preVersionId + ", author is " + author;
  }

  
  
}

b. Entities Bean

package locationService.beans;

import javax.persistence.*;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "entities")
public class Entities {
  private int entityId;
  private String label;
  public enum entityType { 
	 location, group; 
  } 
  
  private entityType locationOrGroup;
  @Id
  @Column(name="entity_id")
  public int getEntityId() {
	return entityId;
  }
  public void setEntityId(int entityId) {
	this.entityId = entityId;
  }

  @Column(name="label")
  public String getLabel() {
	return label;
  }
  public void setLabel(String label) {
	this.label = label;
  }

  @Column(name="location_or_group")
  @Enumerated(EnumType.STRING) 
  public entityType getLocationOrGroup() {
	return locationOrGroup;
  }
  public void setLocationOrGroup(entityType locationOrGroup) {
	this.locationOrGroup = locationOrGroup;
  }

  @Override
  public String toString() {
    return "entityId is " + entityId + ", label is " + label + ", locationOrGroup is " + locationOrGroup;
  }

}

2. Call Method

  /**
   * Check whether this entities id has been in the version
   */
  public boolean checkEntitiesPK (Version version, int entityId) {
    Session session = Config.getSessionFactory().openSession();
    session.beginTransaction();
	
    int versionId = version.getVersionId();
    
    Query query = session.createQuery("from Entities where entityId = :entityId and version_id = :versionId");
    query.setParameter("entityId", entityId);
    query.setParameter("versionId", versionId);
    
    @SuppressWarnings("unchecked")
    List<Entities> o = query.list();
    if (o == null || o.size() == 0){
    	return false;
    }

	return true;
  }

  

Hibernate createQuery调用joincolumn

标签:

原文地址:http://www.cnblogs.com/codingforum/p/4328625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!