Hibernate extend entity for same table

前端 未结 5 1059
猫巷女王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:32

    You can have a common superclass for both entities:

    @MappedSuperclass
    public abstract class AbstractTableObject {
     // common mappings
    }
    
    @Entity
    @Table(name = "someTable")
    public class TableObject extends AbstractTableObject {
     // remaining mappings
    }
    
    @Entity
    @Table(name = "someTable")
    @Immutable
    public class SimpleTableObject extends AbstractTableObject {
     // nothing here
    }
    

    Additionally, you can mark the SimpleTableObject entity to be @Immutable as shown above, so that it cannot be persisted or updated accidentally.

提交回复
热议问题