0

I got the issue with hibernate mapping when generate "create table" sql. The property sid is String type but it becomes integer type after generating table acl_entry in database. And i dont know why. This caused the error like:

org.postgresql.util.PSQLException: ERROR: insert or update on table "acl_entry"
violates foreign key constraint "id"
  Detail: Key (sid)=(0) is not present in table "acl_sid".
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryEx
ecutorImpl.java:2157).

Any help is appreciated.

Here is my code:

AclEntry.java:

@Id
@SequenceGenerator(name="aclentry_identifier_seq", sequenceName="aclentry_identifier_seq", allocationSize=1)
@GeneratedValue(strategy = GenerationType.AUTO, generator="aclentry_identifier_seq")
@Column(name = "id", insertable=false, updatable=false)
private Integer id;



@ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "sid", nullable = false)
@ForeignKey(name = "id")
private AclSid aclSid;


public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

AclSid.java:

@Id
    @SequenceGenerator(name="aclsid_identifier_seq", sequenceName="aclsid_identifier_seq", allocationSize=1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="aclsid_identifier_seq")
    @Column(name = "id", insertable=false, updatable=false)
    private Integer id;



    @Column(name = "sid")
    private String sid;


    public Integer getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

My problem is: after the acl_entry generated, its field "sid" is type of integer not the String as expected. This cause to the key(sid) =(0) after generating.

Table acl_entry generated as:

mask integer,
  acl_object_identity integer,
  sid integer NOT NULL

Thanks for reading.

1
  • Your @ForeignKey states that you are referencing the id column, not sid, which is an integer. Commented Dec 13, 2013 at 9:41

1 Answer 1

1

Instead of having

@ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "sid", nullable = false)
@ForeignKey(name = "id")
private AclSid aclSid;

Try setting

@ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "sid", nullable = false, referencedColumnName = "id")
private AclSid aclSid;

ManyToOne and JoinColumn are JPA annotations. ForeignKey is a Hibernate-specific annotation, and I've personally never used it, so chances are you may not need to either :)

Sign up to request clarification or add additional context in comments.

1 Comment

thanks Zoltán. But my problem is: after the acl_entry generated, its field "sid" is type of integer not the String as expected. This cause to the key(sid) =(0) after generating.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.