Avatar billede fredand Forsker
16. marts 2012 - 12:10 Der er 2 kommentarer og
1 løsning

How to map java-field to db-field in ManyToOne?

Hello guys!

I got a question about a mapping between a entity class and a table, I use with hibernate.

The table looks like:

CREATE TABLE ProductComments
(
    id BIGINT NOT NULL AUTO_INCREMENT,
    parentProductComment_id BIGINT,
    text VARCHAR(1024) NOT NULL,
    PRIMARY KEY(id),
    CONSTRAINT ProductComments_ProductComments FOREIGN KEY(parentProductComment_id) REFERENCES ProductComments (id)
        ON DELETE CASCADE
        ON UPDATE CASCADE       
) ENGINE=InnoDB;

The class looks like:

@Entity
@Table(name = "ProductComments")
public class ProductComment implements Serializable
{
    @Id
    @GeneratedValue
    private String id;
    private String text;
   
    @ManyToOne
    private ProductComment parentProductComment;

    public String getId() {
        return id;
    }

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

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text.trim();
    }
   

    public ProductComment getParentProductComment() {
        return parentProductComment;
    }

    public void setParentProductComment(ProductComment parentProductComment) {
        this.parentProductComment = parentProductComment;
    }

}


The problem is that I would like the field "parentProductComment_id" in the databse be called "fk_parentProductCommentId".
But I would like the entity class be as it is.
I guess in some how it must be possible to tell the @ManyToOne to use a certain column with a certain name, but I can not find out how.

Best regards
Fredrik
Avatar billede fredand Forsker
16. marts 2012 - 21:11 #2
Thanks alot!

(How could I miss that!)

Best regards and please give a svar amigo!
/Fredrik
Avatar billede arne_v Ekspert
16. marts 2012 - 21:54 #3
svar
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester