jakartaee / persistence

https://jakartaee.github.io/persistence/
Other
191 stars 55 forks source link

Proposal for new @View annotation #393

Open dimitrisli opened 1 year ago

dimitrisli commented 1 year ago

Although RDBMS Views can be mapped with the JPA @Table annotation currently, it would be helpful to have a specific annotation for that, @View, that can be meaningful in a declarative way to a Developer reading the codebase. Structurally it would behave just like a @Table and it could even be defined as one, for example:

@Target(TYPE) 
@Retention(RUNTIME)
@Table
public @interface View {
//..
}

Both annotations would be interchangeable just like today the @Table annotation can be used for both a Table and a View.

That would mean a definition such as this one: @View(name = "table_name_here") would be possible and it would be the Developer's responsibility to use it correctly since the whole purpose if it is to facilitate Developers reading the declaration of entities in the codebase.

Happy to provide the annotation definition if this proposal gets considered.

sebersole commented 1 year ago

You mean like (granted poorly named) org.hibernate.annotations.Subselect?

sebersole commented 1 year ago

Oh, sorry read too fast. You mean an existing view.

Personally I'm not against this, but I also think its not super important - @Table already works as you say. From the perspective of JDBC calls there is no difference, aside from schema tooling (export, update, etc) ofc. In fact, its that schema tooling aspect where I see the only real benefit.

But, if you want to create a PR adding support for this I'm not against it.

gavinking commented 1 year ago

You mean an existing view.

Yeah I was about to say I figure he means something like:

@Entity
@View(name="Things", query="...")
public class Thing { ... }

So that the view would be automatically exported.

It makes sense.

hantsy commented 1 year ago

JPA Entity itself can used to map an existing view with @Table(as a readable table) directly, only for query. I remember I've used it in a project before, it works.

This @View is for generating view definition in database for developers?

gavinking commented 11 months ago

So @hantsy I actually implemented this idea in Hibernate, which lets you write things like:

 @Immutable @Entity
 @Table(name="summary")
 @View(query="select type, sum(amount) as total, avg(amount) as average from details group by type")
 @Synchronize("details")
 public class Summary {
     @Id String type;
     Double total;
     Double average;
 }

In the end I didn't find it all that satisfying, since the user has to be quite careful of data aliasing effects.

So, I dunno, if there's strong support for this feature, it's certainly doable, but since it's also quite easy and perfectly elegant to to integrate this as a vendor extension to JPA, the need doesn't seem to be especially acute.

t-beckmann commented 10 months ago

Views would really be useful as a secondary table providing computed columns. I tried that and it fails because persisting a new entity then forces insertion into the view. There is no way around this with existing mappings.