0

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!

Glorfindel
  • 21,988
  • 13
  • 81
  • 109

1 Answers1

1

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());
Community
  • 1
  • 1
Yugang Zhou
  • 7,123
  • 6
  • 32
  • 60
  • im trying not to change my ER model, I tend to think that the framework needs to attend to the database model, not the other way around. Can you explain a little bit more about what you mean with "use a query to retrieve Application"? – Gustavo Sgarbi Campos Jul 30 '13 at 12:51
  • @GustavoSgarbiCampos I agree with you, but sometimes compromise has to be made and answer updated. – Yugang Zhou Jul 30 '13 at 13:42
  • thank you very much for your help. I didn't quite get the repository solution, can you explain a little bit more? I tryed to removing the getApplication and setApplication methods, along with the '@ManyToOne' and '@JoinColumn' annotations but the error keeps occuring! – Gustavo Sgarbi Campos Jul 30 '13 at 14:47
  • @GustavoSgarbiCampos Sorry, my fault. I misunderstood the error stack. See the updated answer. – Yugang Zhou Jul 30 '13 at 15:24