0

I am running a Java + Hibernate application on Google App Engine for a while now. The MySQL database is a Google Cloud SQL (First generation) instance. Now I want to upgrade this to a "Second generation" instance.

If I do so, the EntityManager can not be initialized and it says some classes are not defined. Do I need to do any configuration changes?

The first First generation uses MySQL 5.5, the second one uses MySQL 5.7.

  • I ask that you provide more details about the exact errors you are seeing, along with more information about the changes you made to your configuration that caused this issue. – Jordan Jun 6 '17 at 20:14
  • @jan you may consider checking the "Diagnosing Issues with Cloud SQL Instances" [1] documentation page. More information is needed: what did you mean by "if I do so"? [1] cloud.google.com/sql/docs/mysql/diagnose-issues – George Sep 4 '17 at 14:44
0

I would suggest you to check the package names and classes in the persistence.xml configuration file.

<persistence-unit name="Jondow">
<class>com.example.appengine.cloudsql.ClassName</class>

Then somewhere in your code you will retrieve that class, for example on the doGet function:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("Jondow", properties);
EntityManager em = emf.createEntityManager();

Be sure that you defined all the tables on your database in that file and that the there is a class defined somewhere that maps a table in the database in the correct way. I tried a migration from First Generation MySQL 5.5 to Second Generation MySQL 5.7 and I just needed to change the database access on the pom.xml( instance name, user, password ). Then Hibernate has created all the tables on the database. Be sure you have the minimum data populated for your App Engine because Hibernate creates the tables you defined for you and you might have an issue retrieving empty tables. Also be sure that the user you are using has the correct permissions for manipulating the database in the way you need, because HIbernate won’t create the tables if the user has not the correct permissions

Check the next links for a sample project on Hibernate[1] and documentation on Cloud SQL[2], I mixed them and have both ways working with the same pom.xml. I’m sharing a code for the init() function that you can replace on [3] sample, the doGet function remains equal, be sure to check that the String names you are using are correct.

Map<String, String> properties;
@Override
public void init() throws ServletException {

try {
  ApiProxy.Environment env = ApiProxy.getCurrentEnvironment();
  Map<String,Object> attr = env.getAttributes();
  String hostname = (String) attr.get("com.google.appengine.runtime.default_version_hostname");
  String url = hostname.contains("localhost:") ? System.getProperty("cloudsql-local") : System.getProperty("cloudsql");
  properties = new HashMap();
  if (!hostname.contains("localhost:")) {
     properties.put("javax.persistence.jdbc.driver", "com.mysql.jdbc.GoogleDriver");

  } else {
     properties.put("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver");

  }
  properties.put("javax.persistence.jdbc.url", url);
  log("connecting to: " + url);
  try {
    conn = DriverManager.getConnection(url);

  } catch (SQLException e) {
    throw new ServletException("Unable to connect to Cloud SQL", e);

  }
} finally {

}

}

Also you can check [4] and [5] for more on Hibernate. Here [6] you might find some useful information for migrating from First Generation Cloud SQL instance to Second Generation Cloud SQL instance.

[1] https://github.com/GoogleCloudPlatform/appengine-cloudsql-native-mysql-hibernate-jpa-demo-java/blob/master/src/main/java/com/google/appengine/demos/HibernateJpaServlet.java

[2] https://cloud.google.com/appengine/docs/standard/java/cloud-sql/

[3] https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/appengine-java8/cloudsql

[4] https://www.tutorialspoint.com/hibernate/hibernate_quick_guide.htm

[5] https://dzone.com/articles/spring-hibernate-google

[6] https://cloud.google.com/sql/docs/mysql/upgrade-db

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.