Hibernate @OneToOne mapping with a @Where clause

淺唱寂寞╮ 提交于 2019-12-08 17:41:59

问题


Will this work -

@OneToOne()
@JoinColumn(name = "id", referencedColumnName = "type_id")
@Where(clause = "type_name = OBJECTIVE")
public NoteEntity getObjectiveNote() {
  return objectiveNote;
}

This is what I am trying to do - get the record from table note whose type_id is the id of the current object and type_name is OBJECTIVE.

I can't get the above mapping to work. What am I doing wrong here?


回答1:


This just plain does not work, sorry :( You will need to do it as one to many and live with getting a collection with a single element.

If you really want it to work this way, you can trick hibernate by storing both the foreign key ID and the type_name in a join table and telling it that both columns make up the foreign key.




回答2:


Actually you can achieve this by specifying @OneToOne without any @Where, but putting @Where on the referenced entity class. I tested this on Hibernate 4.3.11.

This works if you don't care about any entity objects that do not match your @Where.

If you do care about other entities, you can probably create a subclass entity, put @Where on it and join that subclass. But I have not tested this scenario.



来源:https://stackoverflow.com/questions/4383070/hibernate-onetoone-mapping-with-a-where-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!