Pierre
Pierre

Reputation: 35226

JPA / eclipse-link 2.6.0 / mysql : incorrect generated table name

I created two tables:

@Entity
@Table(name="instanceOfClass")
public class InstanceOfClass {
(...)
    @OneToMany(fetch=FetchType.LAZY)
    public List<InstanceOfDataProperty> getDataProperties() {
        return dataProperties;
    }
(...)
}

and

@Entity
@Table(name="instanceOfDataProperty")
public class InstanceOfDataProperty  {
    (...)
    @ManyToOne(optional=false,fetch=FetchType.LAZY) 
    @JoinColumn(referencedColumnName="instance_id", nullable=false, updatable=false)
    public InstanceOfClass getInstance()
        {
        return this.instance;
        }
    (...)
 }

when getDataProperties() is called, I get the following error in the log

Call: SELECT t1.id, t1.smallContent, t1.VALUE, t1.INSTANCE_id,t1.prop_id, t1.LARGECONTENT_id FROM instanceOfClass_instanceOfDataProperty t0, instanceOfDataProperty t1 WHERE ((t0.InstanceOfClass_id = ?) AND (t1.id = t0.dataProperties_id)) bind => [1 parameter bound]
Query: ReadAllQuery(name="file:/path/to/WEB-INF/classes/_jdbc/mydao" referenceClass=InstanceOfDataProperty sql="SELECT t1.id, t1.smallContent, t1.VALUE, t1.INSTANCE_id, t1.prop_id, t1.LARGECONTENT_id FROM instanceOfClass_instanceOfDataProperty t0, instanceOfDataProperty t1 WHERE ((t0.InstanceOfClass_id = ?) AND (t1.id = t0.dataProperties_id))")

(....) Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'user_me.instanceOfClass_instanceOfDataProperty' doesn't exist

where does this user_me.instanceOfClass_instanceOfDataProperty comes from ? how can I fix this ?

Upvotes: 0

Views: 120

Answers (1)

beosign
beosign

Reputation: 433

I think you have to use the mappedBy value for the @OneToMany annotation in your InstanceOfClass entity, like this:

@Entity
@Table(name="instanceOfClass")
public class InstanceOfClass {
(...)
    @OneToMany(fetch=FetchType.LAZY, mappedBy="instanceOfClass")
    public List<InstanceOfDataProperty> getDataProperties() {
        return dataProperties;
    }
(...)
}

where instanceOfClass should be the name of the getter in the InstanceOfDataProperty class.

From the JavaEE7 pdf, chapter 37.1.5.1:

Bidirectional relationships must follow these rules.

The inverse side of a bidirectional relationship must refer to its owning side by using the mappedBy element of the @OneToOne, @OneToMany, or @ManyToMany annotation.

The mappedBy element designates the property or field in the entity that is the owner of the relationship. The many side of many-to-one bidirectional relationships must not define the mappedBy element. The many side is always the owning side of the relationship.

I did a quick test, with the entities Person and Address (1:N). First I left out the mappedBy, and a table named Person_Address has been created. After adding the mappedBy, a table Address had been created.

Upvotes: 1

Related Questions