-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: JPA annotations sensitive to method order
PostPosted: Thu Dec 11, 2008 5:22 pm 
Newbie

Joined: Wed Dec 10, 2008 8:42 pm
Posts: 3
I was having trouble getting @AttributeOverrides to work correctly for an @Embeddable object, but I figured out the problem. In my attribute for the @Embeddable object, the method order was set/get instead of get/set. When I fixed that, it started working.

Yes, I could have annotated the private members, but I chose to use annotations on the public accessor methods instead, and thus ran into this problem.

This is my @Embeddable class:

Code:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Embedded1 implements Serializable {

    private int valueX;
    private int valueY;

    public Embedded1() {
    }

    public int getValueX() {
        return this.valueX;
    }
    public void setValueX(int valueX) {
        this.valueX = valueX;
    }

    public int getValueY() {
        return this.valueY;
    }
    public void setValueY(int valueY) {
        this.valueY = valueY;
    }
}


And here is my first attempt to embed that in another class:

Code:
import java.io.Serializable;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.AttributeOverrides;
import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "entity1")
public class Entity1 implements Serializable {

    private long id;
    private Embedded1 embedded1;

    public Entity1() {
    }
   
    @Id @GeneratedValue
    @Column(name = "entity1_id")
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    @Embedded
    @AttributeOverrides
    ({
        @AttributeOverride(name="valueX",
                           column=@Column(name="entity1_valueX")),
        @AttributeOverride(name="valueY",
                           column=@Column(name="entity1_valueY")) })
    public void setEmbedded1(Embedded1 embedded1) {
        this.embedded1 = embedded1;
    }
    public Embedded1 getEmbedded1() {
        return this.embedded1;
    }
}


This doesn't work, and produces the following SQL code to create the table:

Code:
    create table entity1 (
        entity1_id bigint generated by default as identity (start with 1),
        valueX integer not null,
        valueY integer not null,
        primary key (entity1_id)
    );


I changed my @Entity class, so that the attribute methods accessing the @Embeddable object were the opposite order:

Code:
    @Embedded
    @AttributeOverrides
    ({
        @AttributeOverride(name="valueX",
                           column=@Column(name="entity1_valueX")),
        @AttributeOverride(name="valueY",
                           column=@Column(name="entity1_valueY")) })
    public Embedded1 getEmbedded1() {
        return this.embedded1;
    }
    public void setEmbedded1(Embedded1 embedded1) {
        this.embedded1 = embedded1;
    }


and it now produces the correct SQL code:

Code:
    create table entity1 (
        entity1_id bigint generated by default as identity (start with 1),
        entity1_valueX integer,
        entity1_valueY integer,
        primary key (entity1_id)
    );


Top
 Profile  
 
 Post subject: JPA annotations work only on attribute get method, not set
PostPosted: Thu Dec 11, 2008 5:28 pm 
Newbie

Joined: Wed Dec 10, 2008 8:42 pm
Posts: 3
I've validated that this sensitivity is not to the order, but rather that the get method must be annotated, not the set method.

Here is an example piece of code that doesn't work as one might expect:

Code:
    @Column(name="entity1_strx")
    public void setStrX(String strX) {
        this.strX = strX;
    }
    public String getStrX() {
        return this.strX;
    }


while the following two examples do work:

Code:
    public void setStrX(String strX) {
        this.strX = strX;
    }
    @Column(name="entity1_strx")
    public String getStrX() {
        return this.strX;
    }


Code:
    @Column(name="entity1_strx")
    public String getStrX() {
        return this.strX;
    }
    public void setStrX(String strX) {
        this.strX = strX;
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.