Gus Campos
Gus Campos

Reputation: 381

Hibernate with annotations: "Repeated column in mapping fo entity" - Composite PK

First of all I'd like to thank everyone that's even reading this.

I'm having a problem with the Hibernate fw in my application. The following figure shows the diagram of the part of the database thats causing the error.

Diagram
(source: akamaihd.net)

My code was generated autmatically by the reverse engineering, and to make it work I had to "show" the code generator that the module_application table is not a many-to-many table (using a cutom reverse engineering strategy). Now, when I run my code, I get the following error:

Error creating Session: org.hibernate.MappingException: Repeated column in mapping for entity: DAL.module_application.ModuleApplication column: application_id (should be mapped with insert="false" update="false")

The ModuleApplication class is as shown below:

@Entity
@Table(name = "module_application", catalog = "domotics")
public class ModuleApplication implements java.io.Serializable
{

    /**
     * 
     */
    private static final long serialVersionUID = 4725933797587110677L;
    private ModuleApplicationId id;
    private Application application;
    private Module module;
    private Set<ModuleApplicationStatus> moduleApplicationStatuses = new HashSet<ModuleApplicationStatus>(0);

    public ModuleApplication()
    {
    }

    public ModuleApplication(ModuleApplicationId id, Application application, Module module)
    {
        this.id = id;
        this.application = application;
        this.module = module;
    }

    public ModuleApplication(ModuleApplicationId id, Application application, Module module, Set<ModuleApplicationStatus> moduleApplicationStatuses)
    {
        this.id = id;
        this.application = application;
        this.module = module;
        this.moduleApplicationStatuses = moduleApplicationStatuses;
    }

    @EmbeddedId
    @AttributeOverrides(
    {
        @AttributeOverride(name = "networkAddress", column = @Column(name = "network_address", nullable = false, length = 45)),
        @AttributeOverride(name = "applicationId", column = @Column(name = "application_id", nullable = false)) 
    })
    public ModuleApplicationId getId()
    {
        return this.id;
    }

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "application_id", nullable = false, insertable = false, updatable = false)
    public Application getApplication()
    {
        return this.application;
    }

    public void setApplication(Application application)
    {
        this.application = application;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "network_address", nullable = false, insertable = false, updatable = false)
    public Module getModule()
    {
        return this.module;
    }

    public void setModule(Module module)
    {
        this.module = module;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "moduleApplication")
    public Set<ModuleApplicationStatus> getModuleApplicationStatuses()
    {
        return this.moduleApplicationStatuses;
    }

    public void setModuleApplicationStatuses(Set<ModuleApplicationStatus> moduleApplicationStatuses)
    {
        this.moduleApplicationStatuses = moduleApplicationStatuses;
    }
}

As we can see, both insert and update are set to false. If anyone knows the exact cause of this error and/or how to solve it, please share the knowledge!

Upvotes: 0

Views: 4326

Answers (1)

Yugang Zhou
Yugang Zhou

Reputation: 7283

I found you may have the same issue in [this post] (Hibernate Mapping Exception : Repeated column in mapping for entity).

Perhaps you may use an surrogate key as id or use a query to retrieve Application.

You're now using a object graph solution and alternatively you could use query solution:

Keep only identities of Application and Module as fields in ModuleApplication. You could use a query to retrieve Application or Module given a ModuleApplication

public class ModuleApplication {

    private ModuleApplicationId id;

    private Set<ModuleApplicationStatus> moduleApplicationStatuses = new HashSet<ModuleApplicationStatus>(0);
}

Application application = applicationRepository.findBy(moduleApplication.getApplicationId());

Upvotes: 1

Related Questions