Ketan
Ketan

Reputation: 1012

Hibernate: How to read entity every time from DB, but never update to DB?

In my Spring-MVC (along with Hibernate) project, I have a table called 'Division' containing all divisions in the institute. It is referenced in other table called 'Assignment'. Here is what I want to achieve? 1. Fetch division data always from database using Hibernate. But never update division data while saving 'Assignments' or any other entity.

I am not using any cache. @Immutable reads data only once from database, but, I want to fetch 'Division' data from database every time I access it.

Upvotes: 0

Views: 416

Answers (2)

Ghokun
Ghokun

Reputation: 3475

You should use something like this:

@ManyToOne
@JoinColumn(name = "column_name", referencedColumnName = "reference",
insertable =  false, updatable = false)
private aaa bbb;

Notice the insertable and updatable parameters.

Upvotes: 2

Rafa
Rafa

Reputation: 2027

If I understood right you have something like:

@Entity
public class Division {
   ...
   @OneToMany
   Set<Assignment> getAssignments(){}
   ...
}

@Entity
public class Assignment{
   ...
   @ManyToOne
   Division getDivision(){}
   ...
}

If that is the case; then you just need a FetchType.EAGER:

@Entity
public class Assignment{
   ...
   @ManyToOne(fetch = FetchType.EAGER)
   Division getDivision(){}
   ...
}

Upvotes: 1

Related Questions