开发者

Spring 3 + Hibernate: Should I use dirty checking, and how do I do so using annotation based transactions?

开发者 https://www.devze.com 2023-02-22 19:56 出处:网络
First off, I\'m new to Spring and I don\'t have开发者_运维百科 my head quite wrapped around how it handles Hibernate transactions, so feel free to teach me a thing or two about it! :D

First off, I'm new to Spring and I don't have开发者_运维百科 my head quite wrapped around how it handles Hibernate transactions, so feel free to teach me a thing or two about it! :D

I'm writing an application with a standard Controller, Service, Data Access, and Persistence layer. So I have e.g. FileController, FileService, FileDao, with the SpringFramework handling Hibernate.

@Service
public class FileService {

    @Autowired
    FileDao fileDao;

    public FileService() {}

    @Transactional
    public File getRootFile() {
            return fileDao.getRootFile();
    }

    @Transactional
    public File getById(long id) {
            return fileDao.getById(id);
    }

    @Transactional
    public void save(File file) {
            fileDao.save(file);
    }
}

I'm also using the OpenSessionInView pattern with an OpenSessionInViewInterceptor.

I have two questions:

  • Should I be using dirty checking with an open session in the View? Would that cause any changes the View could potentially make to the Model to be persisted?
  • If dirty checking is a good idea, how do I do it? It seems that right now, I have to make a save() or update() call, otherwise dirty objects aren't persisted after my controller returns. Thanks in advance!


If you mean optimistic locking, than have a look at Hibernate Documentation: Chapter 11.3. Optimistic concurrency control

Or JPA http://blogs.oracle.com/carolmcdonald/entry/jpa_2_0_concurrency_and

The annotation you need is @Version

@Version
@Column(name = "version", nullable = false, length = 5)
public int getVersion() { ... }
0

精彩评论

暂无评论...
验证码 换一张
取 消