java - Using a view as a join table with Hibernate -


I have two entities which I want to add through a normal string. I have created a scene that I want to use as the join table. When I try to remove an organization, it all works well. Being in hibernation tries to withdraw from the scene which definitely fails. The database used is MySQL.

Then I have found @Entity public class event {... string product ID; Date event data; ...} @ Entity public class stock {... string product ID; ...}

I created a view in MySQL

  See drop view if EVENT_STOCK_VIEW is present; Event EVENT_STOCK_VIEW as Event EVENT_ID, STOCK.STOCK_ID from the event, stock where STOCK.PRODUCT_ID = EVENT.PRODUCT_ID;   

I added the event:

  @ManyToOne (fetch = FetchType.LAZY) @JoinTable (name = "EVENT_STOCK_VIEW", included column = JoinColumn (Name = "EVENT_ID"), inverted zone column = @ joined column (name = "STOCK_ID", updateable = false, insertable = false)) public stock milestone () {this.stock return; }   

and in stock:

  @OneToMany (fetch = fetchType.LAZY) @JoinTable (name = "EVENT_STOCK_VIEW", included column = @) Columns (name) = "STOCK_ID", updatable = false, insertable = false), inverseJoinColumns = JoinColumn (name = "EVENT_ID", updatable = false, insertable = false)) @edderby ("eventDatetime DESC") public list & lt ; Event & gt; GetEvents () {Return events; }   

I've made a few rounds and found it. But the solution is not really good (you have to use the unit between share and event).

Is there any other solution?

I can use the hibernate interceptor and override OnPrepareStatement (string SQL) and check whether the SQL string is removed from EVENT_STOCK_VIEW and returns a dummy command or not. Obviously a hack that I try to avoid.

Can not you do it at all without joining the table? As far as I understand, your relationship is effectively read only, so that the following approach should work properly:

  @ManyToOne (fetch = FetchType.LAZY) @JoinColumn (name = "ProductId", referenced columnname = "product ID", insertable = false, updating = false) public stock milestone () {this.stock return; } ... @OneToMany (fetch = FetchType.LAZY, mapped = "stock") @orderbie ("EventDatabase DESC") Public list & amp; Lt; Event & gt; GetEvents () {Return events; }    

Comments