Skip to content
Advertisement

JPA generated column name doesnt have underscore (column name mismatch)

I have a composite FK which acts as a composite PK. A part of this composite key is Fk to users table and the files are as follows:

public class LkUnitPK implements Serializable {
    public LkUnitPK() {
    }
    
    private BigDecimal parentUnit;
    private BigDecimal childUnit;
    public LkUnitPK(BigDecimal parentUnit, BigDecimal childUnit) {
        this.parentUnit = parentUnit;
        this.childUnit = childUnit;
    }
    
    public boolean equals(Object other) {
        if (other instanceof LkUnitPK) {
            final LkUnitPK otherLkUnitPK = (LkUnitPK) other;
            final boolean areEqual =
                (otherLkUnitPK.parentUnit.equals(parentUnit) && otherLkUnitPK.childUnit.equals(childUnit));
            return areEqual;
        }
        return false;
    }


    public int hashCode() {
        return super.hashCode();
    }


    public void setParentUnit(BigDecimal parentUnit) {
        this.parentUnit = parentUnit;
    }
    @Column(name = "parent_unit")
    public BigDecimal getParentUnit() {
        return parentUnit;
    }

    public void setChildUnit(BigDecimal childUnit) {
        this.childUnit = childUnit;
    }
    @Column(name = "child_unit")
    public BigDecimal getChildUnit() {
        return childUnit;
    }
}

Users Unit looks like below:

@Entity
@Table(name="LK_UNIT"
    ,catalog="ooo"
)
public class LkUnit {
    public LkUnit() {
        super();
    }
    @Column(name = "unit_name")
    private String unitName;
    @Column(name = "active")
    private BigDecimal active;
    @EmbeddedId
    private LkUnitPK id;
    @OneToMany(fetch = FetchType.EAGER, mappedBy="lkUnit")
    private Set<UsersUnit> usersUnit;
    //////////////..
    
    }
    

UsersUnitPK:

public class UsersUnitPK implements Serializable{
    public UsersUnitPK() {
        super();
    }
    private LkUnitPK lkUnitPk;
    private BigDecimal userId;

    public void setLkUnitPk(LkUnitPK lkUnitPk) {
        this.lkUnitPk = lkUnitPk;
    }

    public LkUnitPK getLkUnitPk() {
        return lkUnitPk;
    }

    public void setUserId(BigDecimal userId) {
        this.userId = userId;
    }

    public BigDecimal getUserId() {
        return userId;
    }
}

UsersUnit:

@Entity
@Table(name = "USERS_UNIT", catalog="ooo")
public class UsersUnit implements Serializable {
    public UsersUnit() {
        super();
    }
    
    @EmbeddedId
    private UsersUnitPK id;
        
    @MapsId("lkUnitPk")
    @JoinColumns({
        @JoinColumn(name="parent_unit", referencedColumnName="parentUnit"),
        @JoinColumn(name="child_unit", referencedColumnName="childUnit")
    })
    @ManyToOne
    private LkUnit lkUnit;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @MapsId("userId")
    @JsonBackReference
    private User user;

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

    public UsersUnitPK getId() {
        return id;
    }

    public void setLkUnit(LkUnit lkUnit) {
        this.lkUnit = lkUnit;
    }

    public LkUnit getLkUnit() {
        return lkUnit;
    }

    public void setUser(User user) {
        this.user = user;
    }
    
    public User getUser() {
        return user;
    }
}

User:

@Entity
@Table(name = "USERS", catalog="ooo")
@JsonInclude(Include.NON_NULL)
public class User {
    
    private BigDecimal id;
    ...///...
    private Set<UsersUnit> usersUnit;
    
        @Id
    @Column(name="id")
    public BigDecimal getId() {
        return id;
    }
    public void setId(BigDecimal id) {
        this.id = id;
    }
    
            public void setUsersUnit(Set<UsersUnit> usersUnit) {
            this.usersUnit = usersUnit;
        }
        @Column
        @OneToMany(fetch = FetchType.EAGER,mappedBy = "user",cascade = {CascadeType.ALL})
        @JsonManagedReference
        public Set<UsersUnit> getUsersUnit() {
            return usersUnit;
        }
        
        
        
        ////
}

I get run time exception as follows: Caused By: java.sql.SQLSyntaxErrorException: ORA-00904: “LKUNIT1_”.”PARENTUNIT”: invalid identifier

SELECT
    usersunit0_.user_id        AS user_id1_6_0_,
    usersunit0_.child_unit     AS child_unit0_6_0_,
    usersunit0_.parent_unit    AS parent_unit0_6_0_,
    usersunit0_.child_unit     AS child_unit0_6_1_,
    usersunit0_.parent_unit    AS parent_unit0_6_1_,
    usersunit0_.user_id        AS user_id1_6_1_,
    usersunit0_.child_unit     AS child_unit2_6_1_,
    usersunit0_.parent_unit    AS parent_unit3_6_1_,
    lkunit1_.childunit         AS childunit1_0_2_,
    lkunit1_.parentunit        AS parentunit2_0_2_,
    lkunit1_.active            AS active3_0_2_,
    lkunit1_.unit_name         AS unit_name4_0_2_
FROM
         users_unit usersunit0_
    INNER JOIN lk_unit lkunit1_ ON usersunit0_.child_unit = lkunit1_.childunit
                                   AND usersunit0_.parent_unit = lkunit1_.parentunit
WHERE
    usersunit0_.user_id = :1 
    

From above , these 2 linesin lk_unit table should have underscores

lkunit1_.child_unit         AS childunit1_0_2_,
lkunit1_.parent_unit        AS parentunit2_0_2_,

I added the @columns on both the getters, tried field levels (but got error that @mapsId isnt mentioned), added @embedded on the class but nothing seems to work.

Can someone tell me what is going wrong here? why would the underscore miss on lk_unit tables?

Advertisement

Answer

You have JoinColumns without underscores:

@JoinColumns({
    @JoinColumn(name="parent_unit", referencedColumnName="parentUnit"),
    @JoinColumn(name="child_unit", referencedColumnName="childUnit")
})

Should be:

@JoinColumns({
    @JoinColumn(name="parent_unit", referencedColumnName="parent_unit"),
    @JoinColumn(name="child_unit", referencedColumnName="child_unit")
})
Advertisement