We upgraded from hibernate 4.3 to 5.2.1.Final
We had a persistent class User.java:
@Entity
@Proxy(lazy= false)
@Table(name = "SIGN_USER") @Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User implements Serializable {
private static final long serialVersionUID = 8476685067435231830L;
// Personal details
@Id
@Column(nullable=false,unique=true)
private String emailId;
private String password;
private String payId;
private String businessName;
private String businessType;
private String firstName;
private String lastName;
@OneToMany(targetEntity=Roles.class,fetch = FetchType.EAGER,cascade = CascadeType.ALL)
private Set<Roles> roles = new HashSet<Roles>();
//getters and setters
}And the mapped class Roles.java was defined as:
@Entity
@Proxy(lazy=false)
public class Roles implements Serializable {
private static final long serialVersionUID = 3527144404701118357L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String name;
//getters and setters
} The problem is earlier hibernate had the mapping in DB using the table 'sign_user_roles'
and the columns looked like:
# |SIGN_USER_emailId | roles_id |
|
test@test.com | 10 |
After upgrading the table was altered and the columns were like
# |SIGN_USER_emailId | roles_id | User_emailId |
|
test@test.com | 10 | |
Now earlier SIGN_USER_emailId was the column for reference (Foreign key) but now its User_emailId i.e. the initially it was according to table name now the class name is used which is causing a lot of problems in my system.
So hibernated referred a different column for foreign key, is it a configuration problem or the way hibernate refers tables has changed.
My hibernate.cfg.xml is:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<!-- Connection pool settings -->
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">50</property>
<property name="c3p0.timeout">18000</property>
<property name="c3p0.max_statements">50</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="c3p0.idle_test_period">10</property>
</session-factory>
</hibernate-configuration>Session provider:
public class HibernateSessionProvider {
private static Logger logger = Logger.getLogger(HibernateSessionProvider.class
.getName());
private SessionFactory factory;
private static class SessionHelper {
private static final HibernateSessionProvider provider = new HibernateSessionProvider();
}
private HibernateSessionProvider() {
// configures settings from hibernate.cfg.xml
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure().applySetting("hibernate.hbm2ddl.auto", "update")
.build();
try {
factory = new MetadataSources(registry).addAnnotatedClass(User.class)
.addAnnotatedClass(Roles.class)
.buildMetadata().buildSessionFactory();
} catch (Exception exception) {
logger.error("Error creating hibernate session" + exception);
throw exception;
}finally{
StandardServiceRegistryBuilder.destroy(registry);
}
}
private SessionFactory getFactory() {
return factory;
}
public static SessionFactory getSessionFactory() {
return SessionHelper.provider.getFactory();
}
public static Session getSession() {
return getSessionFactory().openSession();
}