我有以下课程:
@Entity
@Table(name = "elements")
@Inheritance(strategy=InheritanceType.JOINED)
@XmlRootElement
public abstract class Elements implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idelement")
private Integer idElement;
@Basic(optional = false)
@Column(name = "code")
private String code;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "idElement")
@Fetch(value = FetchMode.SUBSELECT)
private Collection<Alarms> alarmsCollection;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "idElement")
@Fetch(value = FetchMode.SUBSELECT)
private Collection<ElementsHistorical> elementsHistoricalCollection;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST, mappedBy = "elementsCollection")
@Fetch(value = FetchMode.SUBSELECT)
private Collection<ElementsGroups> elementsGroupsCollection;
//Constructors, getters and setters
}
@Entity
@Table(name = "alarms")
@XmlRootElement(name = "Alarms")
public class Alarms implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idalarm")
private Integer idAlarm;
@JoinColumn(name = "idelement", referencedColumnName = "idelement")
@ManyToOne(optional = false)
private Elements idElement;
//Constructors, getters and setters
}
我创建了一个带有CRUD操作的平针织webservice和DAO类来处理Alarms
数据。通过此实现,当我调用GET
方法时,我会在Elements
个内部获得整个Alarms
对象。这是DAO方法:
public List<Alarms> getAlarms(){
Session session = SessionUtil.getSession();
Query query = session.createQuery("from Alarms");
List<Alarms> alarms = query.list();
session.close();
return alarms;
}
我不想要这个fetch = FetchType.EAGER
获取类型,因为我只需要Elements
的PK,但经过一些研究后,这是我发现使我的服务工作的唯一方法。
我尝试了this和this方法,但我无法使其发挥作用。
答案 0 :(得分:0)
您可以在查询中使用左外连接提取。
Query query = session.createQuery("from Alarms alarm left outer join fetch alarm.idElement element");
现在你可以保持fetch = FetchType.LAZY(这是默认值)。
答案 1 :(得分:0)
尝试将@XmlTransient注释添加到要忽略的Elements类中的字段。 像这样:
@Entity
@Table(name = "elements")
@Inheritance(strategy=InheritanceType.JOINED)
@XmlRootElement
public abstract class Elements implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idelement")
private Integer idElement;
@Basic(optional = false)
@Column(name = "code")
@XmlTransient
private String code;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "idElement")
@Fetch(value = FetchMode.SUBSELECT)
@XmlTransient
private Collection<Alarms> alarmsCollection;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "idElement")
@Fetch(value = FetchMode.SUBSELECT)
@XmlTransient
private Collection<ElementsHistorical> elementsHistoricalCollection;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST, mappedBy = "elementsCollection")
@Fetch(value = FetchMode.SUBSELECT)
@XmlTransient
private Collection<ElementsGroups> elementsGroupsCollection;
//Constructors, getters and setters
}
答案 2 :(得分:0)
您可以将多选与Tuple一起使用,仅用于选择特定列。
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<Alarms> root = cq.from(Alarms.class);
CollectionJoin<Object, Object> joinCollection = root.joinCollection("alarmsCollection");
cq.multiselect(joinCollection.get("idElement"));
List<Tuple> tupleResult = entityManager.createQuery(cq).getResultList();
for (Tuple t : tupleResult) {
Long idElement = (Long) t.get(0);
}