似乎我不知道我缺少什么。运行JUnit Test后,得到了这个
Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: user lacks privilege or object not found: DTB_TABLENAME {Insert into ....
这与我的数据库有关吗?还是代码本身?
这里有一些代码:
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
<persistence-unit name="myrestapi" transaction-type="JTA">
<class>com.rest.entity.TableNameBean</class>
<jta-data-source>MyDatasource</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
MyGenericDao
public abstract class MyGenericDao<T, ID extends Serializable> implements GenericDao<T, ID> {
private Class<T> persistentClass;
@SuppressWarnings("unchecked")
public MyGenericDao() {
this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
}
public abstract EntityManager getEntityManager();
public Class<T> getPersistentClass() {
return persistentClass;
}
//....
//....
@SuppressWarnings("unchecked")
public T findById(ID id, boolean lock) {
if(lock)
return getEntityManager().find(getPersistentClass(), id, LockModeType.PESSIMISTIC_WRITE);
else
return getEntityManager().find(getPersistentClass(), id);
}
public T makePersistent(T entity) {
getEntityManager().persist(entity);
return entity;
}
[表名]道
@Singleton
public class TableNameDao extends GenericJpaDao<TableNameBean, Integer>{
@PersistenceContext(unitName="myrestapi")
private EntityManager em;
@Override
public EntityManager getEntityManager() {
return em;
}
}
然后,我的 TableNameDaoTest
public class TableNameDaoTest {
private static EJBContainer container;
@EJB
private TableNameDao tableNameDao;
@Test
public void testFindById() {
TableNameBean tableBean = tableNameDao.findById(1, false);
assertNotNull(tableBean);
}
@Test
public void testFindAll() {
fail("Not yet implemented");
}
@Test
public void testMakePersistent() {
TableNameBean tableBean = new TableNameBean();
tableBean.setLoginName("testname");
tableBean.setPassword("password");
tableNameDao.makePersistent(tableBean);
assertNotNull(tableBean.getAccountId());
}
@BeforeClass
public static void start() {
container = EJBContainer.createEJBContainer();
}
@Before
public void inject() throws NamingException {
container.getContext().bind("inject", this);
}
@AfterClass
public static void stop() {
container.close();
}
对此有任何帮助吗?非常感谢。顺便说一句,我在TomEE上运行,我的数据库也是MySql。我还在tomee.xml
中配置了我的数据库URL,驱动程序,用户名和密码。我还将它导入我的Server文件夹并发布。