0

Could you help me how can I mapping to any entity to db view?

Scenario is here, We create a view on db with native sql we have to do like this and we want to mapping this view to ours entity.

How can we do that? We try to create an entity with same columns on view but it doesn't work?

luffy
  • 315
  • 1
  • 4
  • 22
  • 1
    A view is like a `normal` table, so you can map it in the same way. – Jens Aug 17 '16 at 08:08
  • 1
    Possible duplicate of [Strategies for Mapping Views in NHibernate](http://stackoverflow.com/questions/2963020/strategies-for-mapping-views-in-nhibernate) – DaKirsche Aug 17 '16 at 08:11
  • Try to set mutable="false" in entity class definition – DaKirsche Aug 17 '16 at 08:12
  • 1
    ok thank you so much all of you. We forgot id adding id column to view. We added id column view and entity and we set mutable=false and it works. thank you. – luffy Aug 17 '16 at 08:26
  • Please write up the answer, post it, and accept it. This allows Stack Overflow to archive the question for future use. Alternately, one of the comment contributors could do the write-up and get the credit. – Prune Aug 19 '16 at 21:42

1 Answers1

-1

Hibernate-specific @Immutable annotation which I use in the following code snippet. View can be mapped as like below....

@Entity
@Immutable
public class <NAME>View {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Version
@Column(name = "version")
private int version;

The @Immutable annotation tells Hibernate to ignore all changes on this entity, but you can use it to retrieve data from the database.

List<View> views = em.createQuery("SELECT v FROM View v", View.class).getResultList();