下面列出了javax.persistence.metamodel.ManagedType#getSingularAttributes ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public <T> List<Predicate> byExample(ManagedType<T> mt, Path<T> mtPath, T mtValue, SearchParameters sp, CriteriaBuilder builder) {
List<Predicate> predicates = newArrayList();
for (SingularAttribute<? super T, ?> attr : mt.getSingularAttributes()) {
if (attr.getPersistentAttributeType() == MANY_TO_ONE //
|| attr.getPersistentAttributeType() == ONE_TO_ONE //
|| attr.getPersistentAttributeType() == EMBEDDED) {
continue;
}
Object attrValue = jpaUtil.getValue(mtValue, attr);
if (attrValue != null) {
if (attr.getJavaType() == String.class) {
if (isNotEmpty((String) attrValue)) {
predicates.add(jpaUtil.stringPredicate(mtPath.get(jpaUtil.stringAttribute(mt, attr)), attrValue, sp, builder));
}
} else {
predicates.add(builder.equal(mtPath.get(jpaUtil.attribute(mt, attr)), attrValue));
}
}
}
return predicates;
}
@SuppressWarnings("unchecked")
public <T extends Identifiable<?>, M2O extends Identifiable<?>> List<Predicate> byExampleOnXToOne(ManagedType<T> mt, Root<T> mtPath, T mtValue,
SearchParameters sp, CriteriaBuilder builder) {
List<Predicate> predicates = newArrayList();
for (SingularAttribute<? super T, ?> attr : mt.getSingularAttributes()) {
if (attr.getPersistentAttributeType() == MANY_TO_ONE || attr.getPersistentAttributeType() == ONE_TO_ONE) {
M2O m2oValue = (M2O) jpaUtil.getValue(mtValue, mt.getAttribute(attr.getName()));
Class<M2O> m2oType = (Class<M2O>) attr.getBindableJavaType();
Path<M2O> m2oPath = (Path<M2O>) mtPath.get(attr);
ManagedType<M2O> m2oMt = em.getMetamodel().entity(m2oType);
if (m2oValue != null) {
if (m2oValue.isIdSet()) { // we have an id, let's restrict only on this field
predicates.add(builder.equal(m2oPath.get("id"), m2oValue.getId()));
} else {
predicates.addAll(byExample(m2oMt, m2oPath, m2oValue, sp, builder));
}
}
}
}
return predicates;
}
public <T> List<Predicate> byExample(ManagedType<T> mt, Path<T> mtPath, T mtValue, SearchParameters sp, CriteriaBuilder builder) {
List<Predicate> predicates = newArrayList();
for (SingularAttribute<? super T, ?> attr : mt.getSingularAttributes()) {
if (!isPrimaryKey(mt, attr)) {
continue;
}
Object attrValue = jpaUtil.getValue(mtValue, attr);
if (attrValue != null) {
predicates.add(builder.equal(mtPath.get(jpaUtil.attribute(mt, attr)), attrValue));
}
}
return predicates;
}
/**
* Returns the property name on the given entity type which the given database column is mapped to.
*
* <p>
* If no field on the given type is mapped to this field (which is common in cases of a JPA relationship without an
* actual {@link javax.persistence.Column} annotated field to represent the foreign key) then this method will
* return null.
* </p>
*
* @param entityType the entity type on which to search for a property that is mapped to the given column
* @param databaseColumnName the name of the database column
*
* @return the name of the property on the given entity type which maps to the given column, or null if no such
* mapping exists
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
protected String getPropertyNameFromDatabaseColumnName(ManagedType entityType, String databaseColumnName) {
for (SingularAttributeImpl attr : (Set<SingularAttributeImpl>) entityType.getSingularAttributes()) {
if (!attr.isAssociation()) {
if (!(attr.getClass().isAssignableFrom(EmbeddableTypeImpl.class)) &&
!(attr.getMapping().getClass().isAssignableFrom(AggregateObjectMapping.class)) &&
attr.getMapping().getField().getName().equals(databaseColumnName)) {
return attr.getName();
}
}
}
return null;
}