Java 表对于JPA中的EntityManaged不可见
我已经在mysqladmin控制台中创建了一个表,用一些日期填充它,现在我想用jpahbernate连接到这个数据库并查询它。所以我创建了实体Java 表对于JPA中的EntityManaged不可见,java,mysql,hibernate,jpa,Java,Mysql,Hibernate,Jpa,我已经在mysqladmin控制台中创建了一个表,用一些日期填充它,现在我想用jpahbernate连接到这个数据库并查询它。所以我创建了实体 @Entity @Table(name = "Service") public class Service implements Serializable { @Id @GeneratedValue private Long id; //blablba } 我能够将EntityManager注入CDIBean,但在or
@Entity
@Table(name = "Service")
public class Service implements Serializable {
@Id
@GeneratedValue
private Long id;
//blablba
}
我能够将EntityManager注入CDIBean,但在org.h2.jdbc.JdbcSQLException上查询失败:找不到表服务;SQL语句
我还尝试连接工具和表是正确显示在那个里,我可以查询它。还请注意,我已将javax.persistence.jdbc.user设置为不存在的用户,但仍然得到相同的结果,因此问题可能在于连接-但在这种情况下,我没有得到关于找不到表的异常。问题实际上是由Wildfly引起的-部署的项目使用ExampleDS数据源,它完全忽略了我的请求pesistence.xml。所以我用正确的值创建了新的数据源,在persistence.xml中添加了对它的引用,现在它可以工作了。但我仍然想知道如何在不配置专有数据源的情况下连接到DB。使用WildFly 8.1.0 如果我使用下面的代码,我会发现一个奇怪的表没有找到
@Stateless
public class ClientiPersoaneFiziceFacade extends AbstractFacade<ClientiPersoaneFizice>
{
private EntityManager em;
@PersistenceUnit(unitName = "ManufacturerMaintainanceWPU")
private EntityManagerFactory emf;
}
我的解决方案是使用以下代码:
@Stateless
public class ClientiPersoaneFiziceFacade extends AbstractFacade<ClientiPersoaneFizice>
{
private EntityManager em;
private EntityManagerFactory emf;
@Override
protected EntityManager getEntityManager()
{
if (emf == null)
{
emf = Persistence.createEntityManagerFactory("ManufacturerMaintainanceWPU");
}
if (em == null)
{
em = emf.createEntityManager();
}
return em;
}
}
我的persistence.xml是这样的
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ManufacturerMaintainanceWPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>entities.ClientiPersoaneFizice</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/xxxx?zeroDateTimeBehavior=convertToNull"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="user"/>
<property name="hibernate.connection.password" value="password"/>
</properties>
</persistence-unit>
</persistence>
您确定该用户具有表服务所需的权限吗?是的,我授予了它们。我试图从控制台和DBInspect以该用户的身份登录?检查服务表是否在用户架构中,如果不是,您可以检查权限或在配置文件中设置默认架构我忘了提到我试图使用根帐户访问DB,结果相同。我意识到问题在于连接,不是用户-我使用不存在的用户帐户进行了尝试,并将其放入persistence.xml中,我得到了相同的结果。请添加注释以提供修复的一些详细信息。在我的情况下,Wildfly提出了一个找不到的表,如果我使用这个@persistenceUnitName=ManufacturerMaintenanceWPU,解决方案将显示在我的帖子中。
@Stateless
public class ClientiPersoaneFiziceFacade extends AbstractFacade<ClientiPersoaneFizice>
{
private EntityManager em;
private EntityManagerFactory emf;
@Override
protected EntityManager getEntityManager()
{
if (emf == null)
{
emf = Persistence.createEntityManagerFactory("ManufacturerMaintainanceWPU");
}
if (em == null)
{
em = emf.createEntityManager();
}
return em;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ManufacturerMaintainanceWPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>entities.ClientiPersoaneFizice</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/xxxx?zeroDateTimeBehavior=convertToNull"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="user"/>
<property name="hibernate.connection.password" value="password"/>
</properties>
</persistence-unit>
</persistence>