1

I had tables like Customer, Request and so on and each had ID field; I have now an abstract @MappedSuperclass instead that have

@Id @GeneratedValue private Long id

field, and all tables extends it with @AttributeOverride(name="id", column=@Column(name="ID_CUSTOMER"). but now, when I'm trying to add customer to a table, I'm getting exception: Column ID_Customer cannot accept Null value. when each table had it's own id fields, all works fine. what's wrong? Thanks

@MappedSuperclass
public abstract class GeneralEntity implements Serializable,Cloneable{

@Id
@GeneratedValue
@Column(name = "ID")
private Long id;

@Column(name = "CREATED",nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date created;

@Column(name = "MODIFIED")
@Temporal(TemporalType.TIMESTAMP)
private Date modified;
//getters,setters 

@Entity
@Table(name = "CUSTOMER")
@AttributeOverrides({
@AttributeOverride(name = "id", column = @Column(name="ID_CUSTOMER")),
@AttributeOverride(name="created",  column=@Column(name="CUSTOMER_REGISTERED",nullable = false))
})
public class Customer extends GeneralEntity{

//    @Column(name = "ID_CUSTOMER")
//    @Id
//    @GeneratedValue
//    private Long id;

@Column(name = "CUSTOMER_EMAIL", nullable = false, length = 25, unique = true, updatable = true)
private String email;

@Column(name="CUSTOMER_PASSWORD", nullable = false)
private String password;
//getters setters
Vadim
  • 105
  • 1
  • 9
  • 1
    why is there a need to have customized IDs at all? using inheritance here for a generic ID in a mapped super class is fine for most situations? – MWiesner Jan 16 '15 at 15:28
  • I just wanted to get rid of id field in each `@Entity ` class and extend it from abstract superclass. In each class I change name of ID field with `@AttributeOverride ` because my system uses this column names now and I just wanted to get rid of common fields – Vadim Jan 16 '15 at 15:33
  • Using common fields is useful as you avoid "repeat yourself" problems. Your approach only makes sense to me, if your DB schema is "fix" it makes sense to have variants of ID names. Could you post a more complete example of your inheritance/class model? This would improve your question and give others a chance to provide a useful answer. – MWiesner Jan 16 '15 at 15:40
  • It's strange, but it seems to work now, I just commented other extended @Entity's annotations to have only one Entity Customer. But I'm interested in resolving such magic :o – Vadim Jan 16 '15 at 15:54

0 Answers0