Search code examples
javasql-serverazurejpaeclipselink

Specifying database name with Eclipselink to SQLServer db?


I'm new to ElcipseLink and JPA. I've successfully set up my persistence.xml and EclipseLink library. However, when trying to retrieve data from the server, I get invalid object name: dbo.Material.

My values are stored in the leermiddelen database. The scheme is dbo and the table is Material. When I try to specify the cataloge name (leermiddelen), then I get an error that specifying the database name is not supported in Azure.

A little help? (btw, when manually connection with a connection string and the driver from Microsoft, it does work)

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <class>models.Material</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
        <property name="javax.persistence.jdbc.url"
                  value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
        <property name="javax.persistence.jdbc.user" value="xxxxxxxxxxxx"/>
        <property name="javax.persistence.jdbc.password" value="xxxxxxxxxx"/>
    </properties>
</persistence-unit>

and the java code:

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("test");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    EntityTransaction userTransaction = entityManager.getTransaction();
    try {
        userTransaction.begin();
        @SuppressWarnings("unchecked")
        List<Material> materials = entityManager.createQuery("SELECT e from Material e").getResultList();
        System.out.println("Getting materials list...");
        for (Iterator<Material> iterator = materials.iterator(); iterator.hasNext(); ) {
            Material m = (Material) iterator.next();
            System.out.println(m.toString());
        }
        entityManager.getTransaction().commit();
    } catch (Exception e) {
        entityManager.getTransaction().rollback();
    }
    entityManager.close();
    entityManagerFactory.close();
}

Finally the error:

[EL Warning]: 2016-04-05 11:16:13.426--UnitOfWork(1690796457)--Exception        [EclipseLink-4002] (Eclipse Persistence Services - 2.6.2.v20151217-774c696):  org.eclipse.persistence.exceptions.DatabaseException
 Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'dbo.Material'.
Error Code: 208
Call: SELECT Number, Amount, CanReserve, Description, Firm, IsAvailable, Name, PhotoUrl, Price FROM dbo.Material

Solution

  • Conclusion for the question. It seems that the issue was caused by the incorrect jdbc url. The jdbc url for Azure SQL Database is as below.

    jdbc:sqlserver://<hostname>.database.windows.net:1433;database=<database-name>;user=<username>@<hostname>;password={your_password_here};encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
    

    For getting the connection string on Azure portal, please see https://azure.microsoft.com/en-us/documentation/articles/sql-database-develop-java-simple-windows/#step-4-get-connection-string.