Question

I have a problem with my Hibernate making assumptions on what to call columns.

Specifically, when I do a @ManyToOne field, where I refer to a column in the other Table. What happens is that, If I do not enter a @JoinColumn annotation as well, it maps the field with an underscore in it's name.

For example, i have this class:

@Entity
public class User extends AbstractEntity {

     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private int id;

     private String username;
     private String password;

     @OneToMany(fetch = FetchType.LAZY, mappedBy = "userFK")
     private List<TwitterAccount> twitterAccounts;

     /* GETTERS & SETTERS OMITTED */
}

And then I have the TwitterAccount class:

@Entity
public class TwitterAccount extends AbstractEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @ManyToOne(fetch = FetchType.LAZY)
    private User userFK;
}

What happens is that when it tries to get the Twitter accounts for a certain user, I get the exception: MySQLSyntaxErrorException: Unknown column 'twitteracc1_.userFK_id' in 'field list'

Look at what it tries to map the userFK to: userFK_id. Of course it doesn't exist! I haven't given it that name.

So the question comes down to: Is it possible to turn off this functionality? (The functionality that turns the column name into 'field_'foreignkey')

I am aware that using @JoinColumn(name = "userFK") would solve it, but I'd rather turn it off instead.

Regards

Was it helpful?

Solution

This is the default as specified by the JPA specification

The concatenation of the following: the name of the referencing relationship property or field of the referencing entity or embeddable class; ""; the name of the referenced primary key column. If there is no such referencing relationship property or field in the entity, or if the join is for an element collection, the join column name is formed as the concatenation of the following: the name of the entity; ""; the name of the referenced primary key column.

In hibernate this is implemented in a NamingStrategy in this case the EJB3NamingStrategy. You can implement your own version of this deciding whatever you want to use. But that will probably only complicate/confuse people (which might expect the standards to apply).

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