Question

I have the follow legacy database structure and content:

order parameter        value
--------------------------------
1     user_login       user
1     user_password    123456
1     user_level       basic
2     user_login       admin
2     user_password    s3k42k
2     user_level       advanced

I would like to represent this as below and utilize a entity called 'User' so that i can abstract the complexity of the legacy structure:

id    user  password  level
-------------------------------
1     user  123456    basic
2     admin s3k42k    advanced

I got it do this using a view in database, but i couldnt update using this view. Is there a way to represent the legacy structure using Hibernate so than i can update too?

Was it helpful?

Solution

Your best approach probably is to normalize the table. It will make things much easier in the long run. One thing you could try it this is not an option, is to create another table called user that holds at least a primary key and the id of the users information in your parameter table. This would allow you to treat the relationship as a one-to-many and map it with hibernate. For example you could do something like the following:

Note I might have the mapping wrong I just did this off of the top of my head to illustrate the point.

@Entity
class Parameter{

    @Column(name="order")
    private int order;

    @column(name="parameter")
    private String parameter;

    @column(name="value")
    private String value;

} 

@Entity
class User{

    @column(name="id")
    private int id;

    @OneToMany(cascade=ALL)
    @JoinTable(name="SECONDARY", 
        joinColumns={@JoinColumn(name="id")},
        inverseJoinColumns=@JoinColumn(name="order"))
    @MapKeyJoinColumn(name="parameter")
    private Map<String,Parameter> userData;

    public String getUserName(){
        return userdata.get("user_login")
    }

    etc.

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top