Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaBean持久化模式_Java_Jpa - Fatal编程技术网

JavaBean持久化模式

JavaBean持久化模式,java,jpa,Java,Jpa,我是JPA新手,我正在尝试一些入门示例项目。我尝试了GraniteDS示例项目(“Hello,World”应用程序),发现了一个更新或插入JavaBean的方法Hello。它看起来对我来说有点不好,但另一方面,我不确定它应该如何看起来更好 更新 我不喜欢查询必须抛出异常才能知道不存在具有Welcome实体的结果。有没有办法以优雅的方式检查是否有任何记录 package info.alekna.project.services; import java.util.List; import

我是JPA新手,我正在尝试一些入门示例项目。我尝试了GraniteDS示例项目(“Hello,World”应用程序),发现了一个更新或插入JavaBean的方法
Hello
。它看起来对我来说有点不好,但另一方面,我不确定它应该如何看起来更好

更新 我不喜欢查询必须抛出异常才能知道不存在具有
Welcome
实体的结果。有没有办法以优雅的方式检查是否有任何记录

    package info.alekna.project.services;

import java.util.List;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.NoResultException;

import org.granite.tide.data.DataEnabled;
import org.granite.tide.data.DataEnabled.PublishMode;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import info.alekna.project.entities.Welcome;


@Service
@DataEnabled(topic="welcomeTopic", publish=PublishMode.ON_SUCCESS)
public class WelcomeServiceImpl implements WelcomeService {

    @PersistenceContext
    private EntityManager entityManager;


    @Transactional
    public Welcome hello(String name) {
        if (name == null || name.trim().length() == 0)
            throw new RuntimeException("Name cannot be null or empty");

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Welcome welcome = null;
        try {
            Query q = entityManager.createQuery("select w from Welcome w where w.name = :name");
            q.setParameter("name", name);
            welcome = (Welcome)q.getSingleResult();
            welcome.setMessage("Welcome " + name + " (" + sdf.format(new Date()) + ")");
        }
        catch (NoResultException e) {
            welcome = new Welcome();
            welcome.setName(name);
            welcome.setMessage("Welcome " + name + " (" + sdf.format(new Date()) + ")");
            entityManager.persist(welcome);
        }
        return welcome;
    }


    @Transactional(readOnly=true)
    public List<Welcome> findAll() {
        return entityManager.createQuery("select w from Welcome w order by w.name", Welcome.class).getResultList();
    }
}
package info.alekna.project.services;
导入java.util.List;
导入java.util.Date;
导入java.text.simpleDataFormat;
导入javax.persistence.Query;
导入javax.persistence.EntityManager;
导入javax.persistence.PersistenceContext;
导入javax.persistence.NoResultException;
导入org.granite.tide.data.DataEnabled;
导入org.granite.tide.data.DataEnabled.PublishMode;
导入org.springframework.stereotype.Service;
导入org.springframework.transaction.annotation.Transactional;
导入info.alekna.project.entities.Welcome;
@服务
@DataEnabled(topic=“welcomeTopic”,publish=PublishMode.ON\u成功)
公共类WelcomeServiceImpl实现WelcomeService{
@持久上下文
私人实体管理者实体管理者;
@交易的
公众欢迎你好(字符串名称){
如果(name==null | | name.trim().length()==0)
抛出新的RuntimeException(“名称不能为null或空”);
SimpleDataFormat sdf=新的SimpleDataFormat(“dd/MM/yyyy HH:MM:ss”);
欢迎=null;
试一试{
Query q=entityManager.createQuery(“从欢迎w中选择w,其中w.name=:name”);
q、 setParameter(“名称”,名称);
欢迎=(欢迎)q.getSingleResult();
welcome.setMessage(“welcome”+name+(“+sdf.format(new Date())+”)”);
}
捕获(noresulte异常){
欢迎=新的欢迎();
欢迎使用.setName(name);
welcome.setMessage(“welcome”+name+(“+sdf.format(new Date())+”)”);
entityManager.persist(欢迎);
}
欢迎回来;
}
@事务(只读=真)
公共列表findAll(){
返回entityManager.createQuery(“从Welcome w中选择w,按w.name排序”,Welcome.class)。getResultList();
}
}

好的,您可以在
查询中调用
getResultList()
,检查生成的
列表的
size()
是否正好是一个

但是如果您完全确定查询必须只返回一个结果,那么调用
getSingleResult()
并捕获
NoResultException
ununiqueresultexception
是有意义的-如果发生了这些异常中的任何一个,那是因为只返回一个结果的假设是错误的,并且应该将其作为错误处理。

(回答评论中的问题) 您可以始终使用
q.getResultList()
并检查列表的大小。这样做的问题是,它会导致在表上完全运行,如果不需要,应该避免这样做


如果显式调用
getSingleResult
,则抛出
NoResultException是合乎逻辑的,因为它是一种异常状态:-)

您到底在问什么问题?@vainolo我不喜欢该查询必须抛出异常才能知道不存在具有欢迎实体的结果。有没有办法以优雅的方式检查是否有任何记录?