Hibernate and two classes mapped to single table

Viewed 1709

I have a single table that has multiple attributes. I want to have a two classes for accessing those (exclusive) attributes.

@Entity
@Table(name = "MY_TABLE")
public class Foo {

...
    @Column
    @NotNull
    private String fooValue; 
}

@Entity
@Table(name = "MY_TABLE")
public class Bar {
...
    @Column
    private String barValue; 
...
}

Basically Bar is a read only view on some of the values in a table. Foo is used to update other values and Bar is used to view other values (that are created not using Java application).

The trouble is that when I try to save Foo using Spring Data repository:

public interface FooRepository extends JpaRepository<Foo, Long> {
}

I get exception that fooValue is set to null. And I see in logs an insert with NULL for fooValue. I debugged it a little and when I remove Bar completly from the project the save works, when I add it again save of Foo stops working.

So for me it looks like Hibernate picks up Bar class even when I try to save Foo. Why?

My stack is: Spring Boot 1.5.3, Spring Data, Hibernate 5.x

2 Answers
Related