使用Eclipse Link 2.7.4,保持双向一对多关联失败

问题描述 投票:1回答:1

我试图坚持一个简单的双向一对多关系,但使用eclipse链接2.7.4它失败了。以下是示例代码..

部门实体(家长)

@Entity(name = "Department")
@Table(name = "department")
@XmlAccessorType(XmlAccessType.NONE)
@Cacheable(false)
@XmlRootElement(name = "Department")
public class Department {

    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @XmlElement(name = "id")
    private long id;


    @OneToMany(targetEntity = Employee.class, cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id")
    @XmlElement(name = "employees", type = Employee.class)
    private List<Employee> employees;

    @Version
    @XmlElement(name = "version")
    private long version;

    public long getVersion() {
        return this.version;
    }

    public long getId() {
        return this.id;
    }

    public void setId(final long id) {
        this.id = id;
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }

}

员工实体(儿童)

@Entity(name = "Employee")
@Table(name = "employee")
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "Employee")
public class Employee {

    public Employee() {
        super();
    }

    /**
     * The id.
     */
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @XmlElement(name = "id")
    private long id;

    @ManyToOne(optional = false, targetEntity = Department.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    private Department department;

    @Version
    @XmlElement(name = "version")
    private long version;

    public long getVersion() {
        return this.version;
    }


    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

}

考试班

public class Test {
    private static final String PERSISTENCE_UNIT_NAME = "pun";
    private static EntityManagerFactory factory;

    public static void main(String[] args) {
        factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
        EntityManager em = factory.createEntityManager();

        em.getTransaction().begin();
        Department department = new Department();
        List<Employee> dists = new ArrayList<>();

        Employee emp1 = new Employee();
        emp1.setDepartment(department);
        dists.add(emp1);

        Employee emp2 = new Employee();
        emp2.setDepartment(department);
        dists.add(emp2);

        department.setEmployees(dists);
        em.persist(department);
        em.getTransaction().commit();

        em.close();
    }
}

persistence.xml(使用Derby Embedded DB)

<?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="pun" transaction-type="RESOURCE_LOCAL">
        <!--  <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> -->
        <class>demo.eclipselink.error.Department</class>
        <class>demo.eclipselink.error.Employee</class>
         <properties>
          <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
          <property name="javax.persistence.jdbc.url"
            value="jdbc:derby:/databases/simpleDb;create=true" />
          <property name="javax.persistence.jdbc.user" value="test" />
          <property name="javax.persistence.jdbc.password" value="test" />

          <!-- EclipseLink should create the database schema automatically -->
          <property name="eclipselink.ddl-generation" value="create-tables" />
          <property name="eclipselink.ddl-generation.output-mode" value="database" />

          <property name="eclipselink.logging.level.sql" value="FINE"/>
        <property name="eclipselink.logging.parameters" value="true"/>
         </properties>

    </persistence-unit>
</persistence>

一旦我使用Eclipse Link 2.7.4运行代码,它就会给我以下异常堆栈跟踪。

Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.4.v20190115-ad5b7c6b2a): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Column name 'DEPT_ID' appears more than once times in the column list of an INSERT statement.
Error Code: 20000
Call: INSERT INTO employee (dept_id, id, VERSION, dept_id) VALUES (?, ?, ?, ?)
        bind => [1, 0, 1, 1]

我无法使用v 2.7.3重现此问题,仅在2.7.4中失败。我们正在使用eclipse链接以及Payara 5.191,这个问题也存在。

如果有解决方法,请告诉我们。

谢谢

jpa eclipselink payara
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.