Google Cloud SQL + JPA without WTP



我已经设法从Java连接到Google Cloud SQL数据库。目前,我一直在这样验证这一点:

Connection conn = DriverManager.getConnection(getUrl());

然后从该陈述中取回准备好的陈述。getUrl()是一种以Google指定格式返回URL的方法:" JDBC:Google:mysql://xxx/xxx/xxx?user = xxx& passwork = xxx"。

我想在数据库中使用JPA。查看Google的文档,他们似乎推荐WTP A.K.A. Eclipse Web工具平台(https://developers.google.com/eclipse/eclipse/docs/docs/cloudsql-jpatools)。我不使用Eclipse,我真的很想避免将此工具用于通常应该很容易实现的东西,而无需任何GUI/Wizard/插件。

我的Java Servlet是在春季写的,我的应用程序配置看起来像这样:

@EnableWebMvc
@EnableScheduling
@EnableTransactionManagement
@EnableJpaRepositories("stuff")
@ComponentScan("stuff")
@Configuration
public class AppConfig {
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource driver = new DriverManagerDataSource();
        driver.setDriverClassName("org.postgresql.Driver"); //Obviously not working for Google Cloud SQL
        driver.setUrl(Config.getUrl()); // Same url as described above.
        driver.setUsername(/*omitted*/);
        driver.setPassword(/*omitted*/);
        return driver;
    }
    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory());
        return txManager;
    }
    @Bean
    public EntityManagerFactory entityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("stuff");
        factory.setPersistenceUnitName("PU");
        factory.setDataSource(dataSource());
        factory.afterPropertiesSet();
        return factory.getObject();
    }
}

我的persistence.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<persistence
        xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
        version="2.0">
    <persistence-unit name="PU">
        <class>stuff.Message</class>
        <properties>
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        </properties>
    </persistence-unit>
</persistence>

有什么办法可以修改我的appconfig.java persistence.xml以与Google Cloud Cloud SQL一起工作?如前所述,我很想跳过Eclipse插件。

如果您使用的是Appengine,则可能需要阅读https://cloud.google.com/sql/docs/app-engine-connect和https://cloud.google。com/appengine/docs/java/cloud-sql/.

在这里,您似乎正在尝试连接到Google Cloud SQL实例而不使用Google Cloud SQL驱动程序,但是使用Google Connection String,该字符串需要此驱动程序,如其示例中所述:

// Load the class that provides the new "jdbc:google:mysql://" prefix.
Class.forName("com.mysql.jdbc.GoogleDriver");

因此,您的连接字符串未识别(Postgresql驱动程序无济于事)。

如果您不使用附录,请切换到MySQL驱动程序,然后在连接字符串中删除" Google",就像使用标准MySQL连接(使用主机,端口等,而不是实例名称)一样。<</p>

相关内容

  • 没有找到相关文章

最新更新