Does JPA support mapping to sql views?

前端 未结 3 719
野趣味
野趣味 2020-12-04 17:56

I\'m currently using Eclipselink, but I know now days most JPA implementations have been pretty standardized. Is there a native way to map a JPA entity to a view? I am not l

3条回答
  •  不思量自难忘°
    2020-12-04 18:36

    I've been looking into this myself, and I've found a hack that I'm not 100% certain works but that looks promising.

    In my case, I have a FK column in the view that can effectively function as a PK -- any given instance of that foreign object can only occur once in the view. I defined two objects off of that one field: one is designated the ID and represents the raw value of the field, and the other is designated read-only and represents the object being referred to.

    
    @Id
    @Column(name = "foreignid", unique = true, nullable = false)
    public Long getForeignId() {
    ...
    
    @OneToOne
    @JoinColumn(name = "foreignid", insertable=false, updatable=false)
    public ForeignObject getForeignObject() {
    ...
    

    Like I said, I'm not 100% sure on this one (and I'll just delete this answer if it turns out not to work), but it got my code past a particular crash point.

    Dunno if it applies to your specific situation, though. And there's an excellent chance that after 11 months, you no longer care. :-) What the hell, that "Necromancer" badge doesn't just earn itself....

提交回复
热议问题