我尝试使用JPA,EJB和REST服务创建一个示例项目。服务运行良好,我可以发送Post
和GET
个请求。当我尝试通过EJB连接插入简单对象时,我得到了表未找到错误。我尝试手动创建表,但它没有工作tr
这是我的实体类
员工
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
@Id
@Column(name = "emp_id")
private int empId;
@Basic(optional = false)
@Column(name = "name", nullable = false, length = 63)
private String name;
@Basic(optional = false)
@Column(name = "email", nullable = false, length = 255)
private String email;
@Basic(optional = false)
@Column(name = "salary", columnDefinition = "integer default 2500")
private int salary;
}
的persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="EmployeePU" transaction-type="JTA">
<class>com.ams.company.Entity.Employee</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/company" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.ddl-generation-mode" value="ddl_database_generation" />
</properties>
</persistence-unit>
</persistence>
EmployeeManager
@Stateless
public class EmployeeManager {
@PersistenceContext(unitName = "EmployeePU")
private EntityManager entityManager;
public EmployeeManager() {
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public boolean addEmployee(Employee employee) {
entityManager.persist(employee);
return true;
}
}
答案 0 :(得分:1)
基于以上评论的解决方案:
连接到MySQL服务器时,必须使用MySQL JDBC driver。
您不能将JDBC驱动程序用于任何其他品牌的数据库(本例中的问题是使用Apache Derby JDBC驱动程序)。