-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: @OneToMany join table column names changed in Hibernate 5
PostPosted: Mon Aug 01, 2016 6:05 am 
Newbie

Joined: Mon Aug 01, 2016 5:49 am
Posts: 2
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();
}


Top
 Profile  
 
 Post subject: Re: Column's name changed while upgrading to 5.2.1.Final from 4.
PostPosted: Mon Aug 01, 2016 10:49 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Try to change the @OneToMany mapping like this:

Code:
@OneToMany(targetEntity=Roles.class,fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@JoinTable(
   name = "sign_user_roles",
   joinColumns = @JoinColumn(name = "SIGN_USER_emailId "),
   inverseJoinColumns = @JoinColumn(name = "roles_id")
)
private Set<Roles> roles = new HashSet<Roles>();


Top
 Profile  
 
 Post subject: Re: Column's name changed while upgrading to 5.2.1.Final from 4
PostPosted: Tue Aug 02, 2016 3:02 am 
Newbie

Joined: Mon Aug 01, 2016 5:49 am
Posts: 2
Thanks, it works fine, but I have a doubt.

I need to implement second level cache on these entities too and even without cache I need the best performance.Can you please suggest? The cache provider would be ehcache using the annotation:@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.