本文整理汇总了Java中javax.persistence.NonUniqueResultException类的典型用法代码示例。如果您正苦于以下问题:Java NonUniqueResultException类的具体用法?Java NonUniqueResultException怎么用?Java NonUniqueResultException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NonUniqueResultException类属于javax.persistence包,在下文中一共展示了NonUniqueResultException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testFindHistoryQueryReturnsNonDomainHistoryObject
import javax.persistence.NonUniqueResultException; //导入依赖的package包/类
@Test(expected = SaaSSystemException.class)
public void testFindHistoryQueryReturnsNonDomainHistoryObject()
throws Exception {
doThrow(new NonUniqueResultException()).when(namedQuery)
.getSingleResult();
doReturn(namedQuery).when(em).createNamedQuery(any(String.class));
doReturn(namedQuery).when(namedQuery).setParameter(any(String.class),
any());
List<Product> resultNoHistoryObject = Arrays
.asList(domObject_withBusinessKey);
doReturn(resultNoHistoryObject).when(namedQuery).getResultList();
try {
dataService.findHistory(domObject_withBusinessKey);
} catch (SaaSSystemException e) {
String msg = e.getMessage();
assertTrue(msg.indexOf("findHistory loaded Non-History Object") > 0);
throw e;
}
}
示例2: findCode
import javax.persistence.NonUniqueResultException; //导入依赖的package包/类
public T findCode(String code) {
T entity;
String queryString = "Select c FROM " + entityClass.getSimpleName() + " c where c.code = :code1";
Query query = entityManager.createQuery(queryString).setParameter("code1", code);
try {
entity = (T) query.getSingleResult();
} catch (NoResultException | NonUniqueResultException e) {
entity = null;
LOG.log(Level.WARNING, e.getMessage());
}
return entity;
}
示例3: findControlledVocabularyValueByDatasetFieldTypeAndStrValue
import javax.persistence.NonUniqueResultException; //导入依赖的package包/类
/**
* @param dsft The DatasetFieldType in which to look up a
* ControlledVocabularyValue.
* @param strValue String value that may exist in a controlled vocabulary of
* the provided DatasetFieldType.
* @param lenient should we accept alternate spellings for value from mapping table
*
* @return The ControlledVocabularyValue found or null.
*/
public ControlledVocabularyValue findControlledVocabularyValueByDatasetFieldTypeAndStrValue(DatasetFieldType dsft, String strValue, boolean lenient) {
TypedQuery<ControlledVocabularyValue> typedQuery = em.createQuery("SELECT OBJECT(o) FROM ControlledVocabularyValue AS o WHERE o.strValue = :strvalue AND o.datasetFieldType = :dsft", ControlledVocabularyValue.class);
typedQuery.setParameter("strvalue", strValue);
typedQuery.setParameter("dsft", dsft);
try {
ControlledVocabularyValue cvv = typedQuery.getSingleResult();
return cvv;
} catch (NoResultException | NonUniqueResultException ex) {
if (lenient) {
// if the value isn't found, check in the list of alternate values for this datasetFieldType
TypedQuery<ControlledVocabAlternate> alternateQuery = em.createQuery("SELECT OBJECT(o) FROM ControlledVocabAlternate as o WHERE o.strValue = :strvalue AND o.datasetFieldType = :dsft", ControlledVocabAlternate.class);
alternateQuery.setParameter("strvalue", strValue);
alternateQuery.setParameter("dsft", dsft);
try {
ControlledVocabAlternate alternateValue = alternateQuery.getSingleResult();
return alternateValue.getControlledVocabularyValue();
} catch (NoResultException | NonUniqueResultException ex2) {
return null;
}
} else {
return null;
}
}
}
开发者ID:pengchengluo,项目名称:Peking-University-Open-Research-Data-Platform,代码行数:35,代码来源:DatasetFieldServiceBean.java
示例4: getParentID
import javax.persistence.NonUniqueResultException; //导入依赖的package包/类
private static String getParentID(File node, String derivate_id)
throws NoResultException, NonUniqueResultException {
File parent_node = node.getParentFile();
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<MCRFSNODES> query = cb.createQuery(MCRFSNODES.class);
Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
MCRFSNODES fsNode = em.createQuery(query
.where(
cb.equal(nodes.get(MCRFSNODES_.owner), derivate_id),
cb.equal(nodes.get(MCRFSNODES_.name), parent_node.getName()),
cb.equal(nodes.get(MCRFSNODES_.type), "D")))
.getSingleResult();
LOGGER.debug("Found directory entry for {}", parent_node.getName());
em.detach(fsNode);
return fsNode.getId();
}
示例5: setExtItem
import javax.persistence.NonUniqueResultException; //导入依赖的package包/类
public void setExtItem(int billingNo, int demographicNo, String keyVal, String value, Date dateTime, char status) throws NonUniqueResultException {
BillingONExt ext = getClaimExtItem(billingNo, demographicNo, keyVal);
if(ext != null) {
ext.setValue(value);
ext.setDateTime(dateTime);
ext.setStatus(status);
this.merge(ext);
} else {
BillingONExt res = new BillingONExt();
res.setBillingNo(billingNo);
res.setDemographicNo(demographicNo);
res.setKeyVal(keyVal);
res.setValue(value);
res.setDateTime(dateTime);
res.setStatus(status);
this.persist(res);
}
}
示例6: invokeFrame1
import javax.persistence.NonUniqueResultException; //导入依赖的package包/类
protected void invokeFrame1(){
jpaTxnManager.beginTransaction();
try {
//SQL1: select CA_NAME, CA_B_ID, CA_C_ID, CA_TAX_ST from CUSTOMER_ACCOUNT where CA_ID = ?
//Hibernate: select customerac0_.CA_ID as CA1_9_, customerac0_.CA_B_ID as CA5_9_, customerac0_.CA_BAL as CA2_9_, customerac0_.CA_NAME as CA3_9_, customerac0_.CA_TAX_ST as CA4_9_, customerac0_.CA_C_ID as CA6_9_ from CUSTOMER_ACCOUNT customerac0_ where customerac0_.CA_ID=? fetch first 2 rows only
//It is not the bug from GemFireXD dialect when you see the fetch first 2 rows only for getSingleResult.
//It is the hibernate implementation to avoid possible OOME if the setMaxResultSet is not called
//and set the default resultset to 2 rows.
customerAccount = (CustomerAccount)entityManager.createQuery(CUSTOMER_ACCOUNT_QUERY).setParameter("caId", toTxnInput.getAcctId()).getSingleResult();
} catch (NoResultException nre) {
toTxnOutput.setStatus(-711);
throw nre;
} catch (NonUniqueResultException nure) {
toTxnOutput.setStatus(-711);
throw nure;
} catch(RuntimeException re) {
//Any JPA related exceptions are RuntimeException, catch, log and rethrow.
throw re;
}
//SQL2: select C_F_NAME, C_L_NAME, C_TIER, C_TAX_ID from CUSTOMER where C_ID = ?
customer = (Customer)customerAccount.getCustomer();
//SQL3: select B_NAME from BROKER where B_ID = ?
//Hibernate: select broker0_.B_ID as B1_2_0_, broker0_.B_COMM_TOTAL as B2_2_0_, broker0_.B_NAME as B3_2_0_, broker0_.B_NUM_TRADES as B4_2_0_, broker0_.B_ST_ID as B5_2_0_ from BROKER broker0_ where broker0_.B_ID=?
broker = (Broker)customerAccount.getBroker();
}
示例7: invokeFrame1
import javax.persistence.NonUniqueResultException; //导入依赖的package包/类
protected void invokeFrame1(){
jpaTxnManager.beginTransaction();
try {
//SQL1: select CA_NAME, CA_B_ID, CA_C_ID, CA_TAX_ST from CUSTOMER_ACCOUNT where CA_ID = ?
//Hibernate: select customerac0_.CA_ID as CA1_9_, customerac0_.CA_B_ID as CA5_9_, customerac0_.CA_BAL as CA2_9_, customerac0_.CA_NAME as CA3_9_, customerac0_.CA_TAX_ST as CA4_9_, customerac0_.CA_C_ID as CA6_9_ from CUSTOMER_ACCOUNT customerac0_ where customerac0_.CA_ID=? fetch first 2 rows only
//It is not the bug from GemFireXD dialect when you see the fetch first 2 rows only for getSingleResult.
//It is the hibernate implementation to avoid possible OOME if the setMaxResultSet is not called
//and set the default resultset to 2 rows.
customerAccount = (CustomerAccount)entityManager.createQuery(CUSTOMER_ACCOUNT_QUERY).setParameter("caId", toTxnInput.getAcctId()).getSingleResult();
} catch (NoResultException nre) {
toTxnOutput.setStatus(-711);
throw nre;
} catch (NonUniqueResultException nure) {
toTxnOutput.setStatus(-711);
throw nure;
} catch(RuntimeException re) {
//Any JPA related exceptions are RuntimeException, catch, log and rethrow.
throw re;
}
//SQL2: select C_F_NAME, C_L_NAME, C_TIER, C_TAX_ID from CUSTOMER where C_ID = ?
customer = (Customer)customerAccount.getCustomer();
//SQL3: select B_NAME from BROKER where B_ID = ?
//Hibernate: select broker0_.B_ID as B1_2_0_, broker0_.B_COMM_TOTAL as B2_2_0_, broker0_.B_NAME as B3_2_0_, broker0_.B_NUM_TRADES as B4_2_0_, broker0_.B_ST_ID as B5_2_0_ from BROKER broker0_ where broker0_.B_ID=?
broker = (Broker)customerAccount.getBroker();
}
示例8: findUniqueOrNone
import javax.persistence.NonUniqueResultException; //导入依赖的package包/类
/**
* We request at most 2, if there's more than one then we throw a {@link NonUniqueResultException}
* @throws NonUniqueResultException
*/
public E findUniqueOrNone(E entity, SearchParameters sp) {
// this code is an optimization to prevent using a count
sp.setFirstResult(0);
sp.setMaxResults(2);
List<E> results = find(entity, sp);
if (results == null || results.isEmpty()) {
return null;
}
if (results.size() > 1) {
throw new NonUniqueResultException("Developper: You expected 1 result but we found more ! sample: " + entity);
}
return results.iterator().next();
}
示例9: getSingleResult
import javax.persistence.NonUniqueResultException; //导入依赖的package包/类
@SuppressWarnings("rawtypes")
protected Object getSingleResult( Query query )
{
Object result = null;
List objs = query.getResultList();
if ( objs.size() == 1 )
{
result = objs.get( 0 );
}
else if ( objs.size() > 1 )
{
throw new NonUniqueResultException();
}
return result;
}
示例10: hasMarketingPermission
import javax.persistence.NonUniqueResultException; //导入依赖的package包/类
/**
* Checks if the supplier has been granted the permission to sell the
* technical product - a corresponding marketing permission must exist.
*
* @param technicalProduct
* the permission check is done against this technical product
* @param supplier
* for which the permission check is done
* @param ds
* data service, used to execute sql queries
* @param logger
* if not <code>null</code> a thrown
* <code>ObjectNotFoundException</code> will be logged as warning
* to the system log
* @throws OperationNotPermittedException
* thrown if no or multiple marketing permissions are found.
*/
public static void hasMarketingPermission(
TechnicalProduct technicalProduct, Organization supplier,
DataService ds, Log4jLogger logger)
throws OperationNotPermittedException {
Query query = ds
.createNamedQuery("MarketingPermission.findForSupplierIds");
query.setParameter("tp", technicalProduct);
List<String> searchList = new ArrayList<>();
searchList.add(supplier.getOrganizationId());
query.setParameter("orgIds", searchList);
query.setParameter("refType",
OrganizationReferenceType.TECHNOLOGY_PROVIDER_TO_SUPPLIER);
try {
query.getSingleResult();
} catch (NoResultException | NonUniqueResultException e) {
logAndThrowMarketingPermissionException(logger,
String.valueOf(technicalProduct.getKey()),
supplier.getOrganizationId());
}
}
示例11: find
import javax.persistence.NonUniqueResultException; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
@Transactional(readOnly = true)
public <T> T find(final Class<T> type, final Object id) {
return doWithExceptionTranslation(new Callable<T>() {
@Override
public T call() {
if (id instanceof CompoundKey) {
QueryResults<T> results = findMatching(type,
QueryByCriteria.Builder.andAttributes(((CompoundKey) id).getKeys()).build());
if (results.getResults().size() > 1) {
throw new NonUniqueResultException("Error Compound Key: " + id + " on class " + type.getName()
+ " returned more than one row.");
}
if (!results.getResults().isEmpty()) {
return results.getResults().get(0);
}
return null;
} else {
return sharedEntityManager.find(type, id);
}
}
});
}
示例12: getRoleBoByName
import javax.persistence.NonUniqueResultException; //导入依赖的package包/类
protected RoleBo getRoleBoByName(String namespaceCode, String roleName) {
if (StringUtils.isBlank(namespaceCode)
|| StringUtils.isBlank(roleName)) {
return null;
}
Map<String, Object> criteria = new HashMap<String, Object>(3);
criteria.put(KimConstants.UniqueKeyConstants.NAMESPACE_CODE, namespaceCode);
criteria.put(KimConstants.UniqueKeyConstants.NAME, roleName);
criteria.put(KRADPropertyConstants.ACTIVE, Boolean.TRUE);
QueryResults<RoleBo> results =
getDataObjectService().findMatching(RoleBo.class, QueryByCriteria.Builder.andAttributes(criteria).build());
if (results.getResults().isEmpty()) {
return null;
} else if (results.getResults().size() > 1) {
throw new NonUniqueResultException("Finding a role by name should return a unique role, "
+ "but encountered multiple. namespaceCode='" + namespaceCode + "', name='" + roleName +"'");
}
return results.getResults().get(0);
}
示例13: getRoleBoLiteByName
import javax.persistence.NonUniqueResultException; //导入依赖的package包/类
protected RoleBoLite getRoleBoLiteByName(String namespaceCode, String roleName) {
if (StringUtils.isBlank(namespaceCode)
|| StringUtils.isBlank(roleName)) {
return null;
}
Map<String, Object> criteria = new HashMap<String, Object>(3);
criteria.put(KimConstants.UniqueKeyConstants.NAMESPACE_CODE, namespaceCode);
criteria.put(KimConstants.UniqueKeyConstants.NAME, roleName);
criteria.put(KRADPropertyConstants.ACTIVE, Boolean.TRUE);
QueryResults<RoleBoLite> results =
getDataObjectService().findMatching(RoleBoLite.class, QueryByCriteria.Builder.andAttributes(criteria).build());
if (results.getResults().isEmpty()) {
return null;
} else if (results.getResults().size() > 1) {
throw new NonUniqueResultException("Finding a role by name should return a unique role, "
+ "but encountered multiple. namespaceCode='" + namespaceCode + "', name='" + roleName +"'");
}
return results.getResults().get(0);
}
示例14: getByAttribute
import javax.persistence.NonUniqueResultException; //导入依赖的package包/类
/**
* Retrieves a single {@link ResourceEntity} by the given attribute and value.
*
* @param attribute
* The attribute of the resource entity to retrieve it by
* @param value
* The value of the attribute to compare it to
* @param clazz
* The concrete resource entity class to retrieve (may also be {@link ResourceEntity})
* @return The matching {@link ResourceEntity}
* @throws ResourceNotFoundException
* If no {@link ResourceEntity} could be found
* @throws OsiamException
* If more than 1 {@link ResourceEntity} was found
*/
public <T extends ResourceEntity, V> T getByAttribute(SingularAttribute<? super T, V> attribute, V value,
Class<T> clazz) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<T> cq = cb.createQuery(clazz);
Root<T> resource = cq.from(clazz);
cq.select(resource).where(cb.equal(resource.get(attribute), value));
TypedQuery<T> q = em.createQuery(cq);
try {
return q.getSingleResult();
} catch (NoResultException nre) {
throw new ResourceNotFoundException(String.format("Resource with attribute '%s' set to '%s' not found",
attribute.getName(), value), nre);
} catch (NonUniqueResultException nure) {
throw new OsiamException(String.format("Muliple resources with attribute '%s' set to '%s' found",
attribute.getName(), value), nure);
}
}
示例15: loadByField
import javax.persistence.NonUniqueResultException; //导入依赖的package包/类
/**
* 按指定的字段的值加载记录<br>
* 如果指定的字段不是主键,也只返回第一条数据。
*
* @param field
* 作为查询条件的字段
* @param value
* 要查询的值
* @param unique
* 是否要求结果唯一,为true时如结果不唯一将抛出NonUniqueResultException异常
*
* @return 符合条件的记录
* @throws SQLException
* 如果数据库操作错误,抛出。
* @throws NonUniqueResultException
* 结果不唯一
*/
@SuppressWarnings("unchecked")
public <T> T loadByField(jef.database.Field field, Object value, boolean unique) throws SQLException {
ITableMetadata meta = DbUtils.getTableMeta(field);
Query<?> query = meta.newInstance().getQuery();
query.addCondition(field, Operator.EQUALS, value);
List<?> list = typedSelect(query, null, QueryOption.DEFAULT_MAX1);
if (list.isEmpty()) {
return null;
} else if (list.size() > 1 && unique) {
throw new NonUniqueResultException();
}
if (meta.getType() == EntityType.POJO) {
return (T) ((PojoWrapper) list.get(0)).get();
} else {
return (T) list.get(0);
}
}