在JPA中没有创建连接表?

时间:2011-11-09 06:22:27

标签: java java-ee jpa openjpa

解决方案:  我找到了解决这个问题的方法。我使用shell脚本运行我的主类,我也使用相同的shell脚本增强了我的所有类,这里是代码。

#!/bin/sh
#
# Compile and Run the OpenJPA test program
#
cp=.
cp=$cp:lib/mysql-connector-java.jar
cp=$cp:lib/openjpa-2.0.1.jar
cp=$cp:lib/commons-lang-2.1.jar
cp=$cp:lib/geronimo-jta_1.1_spec-1.1.1.jar
cp=$cp:lib/serp-1.13.1.jar
cp=$cp:lib/commons-collections-3.2.1.jar
cp=$cp:lib/geronimo-jpa_2.0_spec-1.1.jar
#
echo "Compiling"
javac -cp $cp *.java */*.java
echo "Enhancing"
java -cp $cp org.apache.openjpa.enhance.PCEnhancer nu/InstrumentType.java nu/Instrument.java nu/Laboratory.java nu/MedicalLaboratory.java
echo "Running"
java -cp $cp test

正如您所见,nu是我的包名,我的课程需要加强,后面是/ 考试是我的主要课程。 我不知道但是增强了这种方式,而不是使用内置增强器的ecplise。

QUESTION :

这是我的文件图层层次结构

This is my file layer hierarchy

我正在使用openjpa

仪器,仪器类型,实验室,医学实验室是我的课程。 我在我的Instrument类和InstrumentType类中创建了未连接的表/引用表。

我的Instrument类中有以下代码

@ManyToOne
    @JoinColumn(name="type", nullable=false)
    public InstrumentType type;


    @ManyToMany(fetch=EAGER)
    @JoinTable(name="usedBy",
           joinColumns=@JoinColumn(name="instrument"),
           inverseJoinColumns=@JoinColumn(name="laboratory"))
    public Set<Laboratory> UsedBy;

public void setUsedBy(Set<Laboratory> UsedBy) {
    this.UsedBy = UsedBy;
    }

我的InstrumentType类中有以下代码

@ElementCollection(fetch=EAGER)
    @Column(name="name")
    @CollectionTable(name="InstrumentName", joinColumns=@JoinColumn(name="type"))
    public Set<String> names;

这是输出,即我在Navicat中可以看到的表格。正如您所看到的,只创建了4个表而不是连接表。

enter image description here

这是我的主要课程

public class main {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {

    // Create the new objects to be persisted.
    Instrument i = new Instrument();
    InstrumentType it = new InstrumentType();
    Laboratory l = new Laboratory();
    MedicalLaboratory ml = new MedicalLaboratory();


    // Configure and create the factory.
    java.util.Map<Object,Object> map = new java.util.HashMap<Object,Object>();
    map.put("openjpa.ConnectionUserName", "root");
    map.put("openjpa.ConnectionPassword", "root");
    map.put("openjpa.ConnectionURL", "jdbc:mysql://127.0.0.1:3306/test123");
    map.put("openjpa.ConnectionDriverName", "com.mysql.jdbc.Driver");
    map.put("openjpa.jdbc.SynchronizeMappings", "buildSchema");
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("test123", map);

        // Create a new EntityManager from the EntityManagerFactory. The
        // EntityManager is the main object in the persistence API, and is
        // used to create, delete, and query objects, as well as access
        // the current transaction
        EntityManager em = factory.createEntityManager();

        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();

        // Create and persist a new Message entity
        em.persist(i);
        em.persist(it);
        em.persist(l);
        em.persist(ml);


        // Commit the transaction, which will cause the entity to
        // be stored in the database
        em.getTransaction().commit();

        // It is always good practice to close the EntityManager so that
        // resources are conserved.
        em.close();
        factory.close();

}
}

这是我无法理解的错误。

73  test123  INFO   [main] openjpa.Runtime - Starting OpenJPA 2.0.1
164  test123  INFO   [main] openjpa.jdbc.JDBC - Using dictionary class "org.apache.openjpa.jdbc.sql.MySQLDictionary".
Exception in thread "main" <openjpa-2.0.1-r422266:989424 fatal store error> org.apache.openjpa.persistence.RollbackException: null
    at org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:584)
    at nu.main.main(main.java:50)
Caused by: <openjpa-2.0.1-r422266:989424 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: null
    at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:1963)
    at org.apache.openjpa.kernel.LocalManagedRuntime.commit(LocalManagedRuntime.java:81)
    at org.apache.openjpa.kernel.BrokerImpl.commit(BrokerImpl.java:1479)
    at org.apache.openjpa.kernel.DelegatingBroker.commit(DelegatingBroker.java:925)
    at org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:560)
    ... 1 more
Caused by: java.lang.IllegalArgumentException
    at nu.MedicalLaboratory.pcProvideField(MedicalLaboratory.java)
    at org.apache.openjpa.kernel.StateManagerImpl.provideField(StateManagerImpl.java:3135)
    at org.apache.openjpa.kernel.StateManagerImpl.preFlush(StateManagerImpl.java:2956)
    at org.apache.openjpa.kernel.PNewState.beforeFlush(PNewState.java:40)
    at org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(StateManagerImpl.java:1047)
    at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:2077)
    at org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:2037)
    at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:1955)
    ... 5 more

我只是想让连接的表格显示出来,以便我可以查询它们。如果有人能指出为什么没有创建连接表,那将是很棒的。

1 个答案:

答案 0 :(得分:1)

下面的例外情况指出了您目前遇到的问题:

at org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:560)
    ... 1 more
Caused by: java.lang.IllegalArgumentException
    at nu.MedicalLaboratory.pcProvideField(MedicalLaboratory.java)

您是否可以发布您拥有的完整映射类,以及您要映射的实体关系是什么?你应该首先建立一个ER类,没有ER字段,然后选择“最简单的”ER关系,然后映射下一个。从您的列表中不清楚,正在映射什么?