本文整理匯總了Java中javax.persistence.TypedQuery.getSingleResult方法的典型用法代碼示例。如果您正苦於以下問題:Java TypedQuery.getSingleResult方法的具體用法?Java TypedQuery.getSingleResult怎麽用?Java TypedQuery.getSingleResult使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.persistence.TypedQuery
的用法示例。
在下文中一共展示了TypedQuery.getSingleResult方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getByName
import javax.persistence.TypedQuery; //導入方法依賴的package包/類
public static Companies getByName(String name) {
if (name != null && !name.isEmpty()) {
EntityManager em = EMFUtil.getEMFactory().createEntityManager();
String query = "SELECT company FROM Companies company where company.name = :name";
TypedQuery<Companies> q = em.createQuery(query, Companies.class);
q.setParameter("name", name);
try {
Companies company = q.getSingleResult();
em.close();
return company;
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
示例2: test201_Insert
import javax.persistence.TypedQuery; //導入方法依賴的package包/類
@Test
public void test201_Insert() throws Exception {
em.getTransaction().begin();
Employee e = new Employee();
e.setEmpNo(99999);
e.setFirstName("John");
e.setLastName("Doe");
e.setBirthDate(DATE_FORMATTER.parse("2008-04-12"));
e.setGender(Gender.MALE);
e.setHireDate(DATE_FORMATTER.parse("2016-01-30"));
e.setSalaries(null);
em.persist(e);
em.getTransaction().commit();
TypedQuery<Employee> q = em.createQuery("select e from employee e where e.empNo = :empNo", Employee.class);
q.setParameter("empNo", 99999);
Employee inserted = q.getSingleResult();
Assert.assertEquals(e, inserted);
}
示例3: cleanUp
import javax.persistence.TypedQuery; //導入方法依賴的package包/類
@AfterClass
public void cleanUp() {
// Delete S3 bucket
DeleteBucketRequest deleteBucketRequest = new DeleteBucketRequest(ContentHelper.getInstance().getConfiguredBucketName());
s3Client.deleteBucket(deleteBucketRequest);
EntityManager _em = Persistence.createEntityManager();
TypedQuery<User> query =_em.createQuery("select u from User u where u.userName=:userName", User.class);
query.setParameter("userName", _user.getUserName());
_user = query.getSingleResult();
query.setParameter("userName", sharedUser.getUserName());
sharedUser = query.getSingleResult();
_em.getTransaction().begin();
_em.remove(_user);
_em.remove(sharedUser);
_em.getTransaction().commit();
}
示例4: buscarUsuario
import javax.persistence.TypedQuery; //導入方法依賴的package包/類
public Usuario buscarUsuario(String apelido) {
StringBuilder sb = new StringBuilder();
sb.append("select u from Usuario u where apelido = '").append(apelido).append("'");
TypedQuery<Usuario> query = super.em.createQuery(sb.toString(), Usuario.class);
try {
return query.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
示例5: getRandomQuizzes
import javax.persistence.TypedQuery; //導入方法依賴的package包/類
public List<Quiz> getRandomQuizzes(int n, long categoryId){
TypedQuery<Long> sizeQuery= em.createQuery(
"select count(q) from Quiz q where q.subCategory.parent.id=?1", Long.class);
sizeQuery.setParameter(1, categoryId);
long size = sizeQuery.getSingleResult();
if(n > size){
throw new IllegalArgumentException("Cannot choose " + n + " unique quizzes out of the " + size + " existing");
}
Random random = new Random();
List<Quiz> quizzes = new ArrayList<>();
Set<Integer> chosen = new HashSet<>();
while(chosen.size() < n) {
int k = random.nextInt((int)size);
if(chosen.contains(k)){
continue;
}
chosen.add(k);
TypedQuery<Quiz> query = em.createQuery(
"select q from Quiz q where q.subCategory.parent.id=?1", Quiz.class);
query.setParameter(1, categoryId);
query.setMaxResults(1);
query.setFirstResult(k);
quizzes.add(query.getSingleResult());
}
return quizzes;
}
示例6: findInternalByName
import javax.persistence.TypedQuery; //導入方法依賴的package包/類
protected Dto findInternalByName(EntityManager manager, String name) throws NotFoundException {
TypedQuery<Dto> query = createFindByNameQuery(name);
try {
return query.getSingleResult();
} catch (NoResultException e) {
throw new NotFoundException(Messages.DEPLOY_TARGET_WITH_NAME_NOT_FOUND, name);
}
}
示例7: login
import javax.persistence.TypedQuery; //導入方法依賴的package包/類
public static Candidates login(String username, String password) {
if (username != null && password != null && !username.isEmpty() && !password.isEmpty()) {
EntityManager em = EMFUtil.getEMFactory().createEntityManager();
String query;
TypedQuery<Candidates> q;
try {
if (username.contains("@")) {
query = "SELECT c FROM Candidates c WHERE c.email = :email";
q = em.createQuery(query, Candidates.class);
q.setParameter("email", username.toLowerCase().trim());
} else {
query = "SELECT c FROM Candidates c WHERE c.phone = :phone";
q = em.createQuery(query, Candidates.class);
q.setParameter("phone", CustomUtilities.cleanNumber(username));
}
Candidates candidate = q.getSingleResult();
if (candidate != null && CustomUtilities.hashPassword(password, CustomUtilities.hexToBytes(candidate.getSalt()))[0].equals(candidate.getPassword())) {
CandidateLoginLogs loginLog = addLoginLog(candidate);
candidate.getLoginLog().add(loginLog);
return candidate;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
em.close();
}
}
return null;
}
示例8: test118_FunctionToDate
import javax.persistence.TypedQuery; //導入方法依賴的package包/類
@Test
public void test118_FunctionToDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
TypedQuery<Date> q = em.createQuery("select to_date('2017-02-23', 'yyyy-MM-dd', :tz) from dual d", Date.class);
q.setParameter("tz", TimeZone.getDefault().getID());
Date result = q.getSingleResult();
Assert.assertEquals("2017-02-23", sdf.format(result));
}
示例9: deleteClient
import javax.persistence.TypedQuery; //導入方法依賴的package包/類
/***Supprime un client de la base de donnee***/
public void deleteClient(Integer id){
TypedQuery<Client> query = em.createQuery("SELECT c FROM Client c WHERE c.id = :id", Client.class);
query.setParameter("id", id);
Client c = query.getSingleResult();
em.remove(c);
}
示例10: getAccount
import javax.persistence.TypedQuery; //導入方法依賴的package包/類
/**
* Find account by its semantic key. Note this will throw {@link NoResultException} which will roll back the
* transaction if the customer is not found.
*/
@LogDuration(limit = 50)
public VirtualAccount getAccount(Long vaNumber) {
TypedQuery<VirtualAccount> q = em.createQuery("select va from VirtualAccount va where va.vaNumber=:number",
VirtualAccount.class);
q.setParameter("number", vaNumber);
return q.getSingleResult();
}
示例11: getCustomer
import javax.persistence.TypedQuery; //導入方法依賴的package包/類
/**
* Find customer by its semantic key. Note this will throw {@link NoResultException} which will roll back the
* transaction if the customer is not found - if this is a problem consider using
* {@link #findCustomer(String)}.
*/
@LogDuration(limit = 50)
public Customer getCustomer(String customerNo) {
TypedQuery<Customer> q = em.createQuery("select c from Customer c where c.sid=:sid", Customer.class);
q.setParameter("sid", customerNo);
return q.getSingleResult();
}
示例12: getAccountByAccountNumber
import javax.persistence.TypedQuery; //導入方法依賴的package包/類
/**
* Find account by names. Note this will throw {@link NoResultException} which will roll back the
* transaction if the account is not found.
*/
@LogDuration(limit = 50)
public VirtualAccount getAccountByAccountNumber(Long vaNumber) {
TypedQuery<VirtualAccount> q = em.createQuery("select va from VirtualAccount va where va.vaNumber=:number",
VirtualAccount.class);
q.setParameter("number", vaNumber);
return q.getSingleResult();
}
示例13: test117_FunctionToCharNumber
import javax.persistence.TypedQuery; //導入方法依賴的package包/類
@Test
public void test117_FunctionToCharNumber() {
TypedQuery<String> q = em.createQuery("select to_char(23.5678, '#0.0') from dual d", String.class);
String result = q.getSingleResult();
Assert.assertEquals("23.6", result.replace(",", "."));
}
示例14: test113_FunctionLength
import javax.persistence.TypedQuery; //導入方法依賴的package包/類
@Test
public void test113_FunctionLength() {
TypedQuery<Integer> q = em.createQuery("select length('abcdef') from dual d", Integer.class);
Integer result = q.getSingleResult();
Assert.assertEquals(Integer.valueOf("abcdef".length()), result);
}
示例15: count
import javax.persistence.TypedQuery; //導入方法依賴的package包/類
public Long count() {
TypedQuery<Long> q = this.entityManager.createQuery(this.getQueryForCount());
return q.getSingleResult();
}