Hibernate extend entity for same table

前端 未结 5 1056
猫巷女王i
猫巷女王i 2021-02-05 22:32

I have a table with two fields I would like to have two objects.

First one only has field1

@Entity(name = \"simpleTableObject\")
@Table(name = \"someTabl         


        
5条回答
  •  臣服心动
    2021-02-05 23:27

    The comment is a bit too long, so I have to write it in the answer.

    Are you going to have two separate DAO to retrieve TableObject and SimpleTableObject separately?If yes, you don't need the extend, just treat them as two separate entities. If not, you can first get the TableObject and maybe create a constructor with SimpleTableObject(TableObject) and copy all the fields you need there. I don't think it is possible to retrieve two entities with one findById function. Hibernate will be confused which entity to map to with the id, which means it doesn't make much sense to have the extend. If you want to keep the consistency, how about considering SimpleTableObject as a view of TableObjec.

    UPDATE : @Quillion I don't think it is possible this way. Since these two entities are basically identical(you can ignore some fields in an entity), there is no way for hibernate to identify which entity to map. Of course you can add an extra column as a discriminator, then use the @Inheritance(strategy=InheritanceType.SINGLE_TABLE) to map these two entities, but I assume this is not what you want.

提交回复
热议问题