我是ElcipseLink和JPA的新手。我已经成功设置了persistence.xml和EclipseLink库。但是,当尝试从服务器检索数据时,我得到无效的对象名称:dbo.Material。
我的值存储在leermiddelen数据库中。该计划是dbo,表格是材料。当我尝试指定目录名称(leermiddelen)时,我收到一条错误,指出Azure中不支持指定数据库名称。
一点帮助? (顺便说一句,当手动连接连接字符串和来自Microsoft的驱动程序时,它确实有效)
<?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>
和java代码:
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();
}
最后是错误:
[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
答案 0 :(得分:1)
问题的结论。似乎问题是由不正确的jdbc url引起的。 Azure SQL数据库的jdbc URL如下所示。
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;
要获取Azure门户上的连接字符串,请参阅https://azure.microsoft.com/en-us/documentation/articles/sql-database-develop-java-simple-windows/#step-4-get-connection-string。