Hibernate createQuery调用joincolumn

时间:2024-09-02 13:04:50

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