Netbeans平台和Jpa EclipsLink

时间:2017-11-04 21:10:59

标签: java jpa eclipselink

我在netbeans上创建了一个项目并添加了jpa,但是当我运行这个项目时,我发现了这个错误。我使用netbeans ide。

  

[EL Info]:2017-11-05 00:01:05.271 - ServerSession(1276666395) - EclipseLink,版本:Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd   [EL Info]:连接:2017-11-05 00:01:05.451 - ServerSession(1276666395) - file:/ C:/ Users / kB / Documents / NetBeansProjects / JavaApplication11 / build / classes / _JavaApplication11PU登录成功   [EL警告]:2017-11-05 00:01:20.582 - ServerSession(1276666395) - 例外[EclipseLink-4002](Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions .DatabaseException   内部异常:java.sql.SQLException:表' room'已经存在   错误代码:1050   调用:CREATE TABLE room(room_id INTEGER NOT NULL,room_name VARCHAR(255),PRIMARY KEY(room_id))   查询:DataModifyQuery(sql =" CREATE TABLE room(room_id INTEGER NOT NULL,room_name VARCHAR(255),PRIMARY KEY(room_id))")   [EL警告]:2017-11-05 00:01:35.687 - ServerSession(1276666395) - 例外[EclipseLink-4002](Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions .DatabaseException   内部异常:java.sql.SQLException:表'序列'已经存在   错误代码:1050   调用:CREATE TABLE SEQUENCE(SEQ_NAME VARCHAR(50)NOT NULL,SEQ_COUNT DECIMAL(38),PRIMARY KEY(SEQ_NAME))   查询:DataModifyQuery(sql =" CREATE TABLE SEQUENCE(SEQ_NAME VARCHAR(50)NOT NULL,SEQ_COUNT DECIMAL(38),PRIMARY KEY(SEQ_NAME))")   [EL警告]:2017-11-05 00:01:35.816 - UnitOfWork(190868662) - java.lang.ClassCastException:java.lang.String无法强制转换为java.lang.Number   线程" main"中的例外情况java.lang.ClassCastException:java.lang.String无法强制转换为java.lang.Number       在org.eclipse.persistence.sequencing.QuerySequence.updateAndSelectSequence(QuerySequence.java:278)       at org.eclipse.persistence.sequencing.StandardSequence.getGeneratedVector(StandardSequence.java:71)       at org.eclipse.persistence.sequencing.DefaultSequence.getGeneratedVector(DefaultSequence.java:163)       at org.eclipse.persistence.sequencing.Sequence.getGeneratedVector(Sequence.java:257)       at org.eclipse.persistence.internal.sequencing.SequencingManager $ Preallocation_Transaction_NoAccessor_State.getNextValue(SequencingManager.java:468)       at org.eclipse.persistence.internal.sequencing.SequencingManager.getNextValue(SequencingManager.java:1067)       at org.eclipse.persistence.internal.sequencing.ClientSessionSequencing.getNextValue(ClientSessionSequencing.java:70)       at org.eclipse.persistence.internal.descriptors.ObjectBuilder.assignSequenceNumber(ObjectBuilder.java:361)       在org.eclipse.persistence.internal.descriptors.ObjectBuilder.assignSequenceNumber(ObjectBuilder.java:320)       at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.assignSequenceNumber(UnitOfWorkImpl.java:486)       at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNotRegisteredNewObjectForPersist(UnitOfWorkImpl.java:4290)       at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.registerNotRegisteredNewObjectForPersist(RepeatableWriteUnitOfWork.java:518)       at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4235)       在org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496)       在javaapplication11.JavaApplication11.main(JavaApplication11.java:29)   C:\ Users \ kB \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml:53:Java返回:1   建筑失败(总时间:31秒)

这是我的实体类

package javaapplication11;

import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 *
 * @author kB
 */
@Entity
@Table(name="room")
public class NewEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "room_id")
    private int id;

    @Column(name = "room_name")
    private String name;

    public NewEntity() {
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 29 * hash + this.id;
        hash = 29 * hash + Objects.hashCode(this.name);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final NewEntity other = (NewEntity) obj;
        if (this.id != other.id) {
            return false;
        }
        if (!Objects.equals(this.name, other.name)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return super.toString(); //To change body of generated methods, choose Tools | Templates.
    }





}

它是主要的课程

package javaapplication11;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author kB
 */
public class JavaApplication11 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaApplication11PU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        NewEntity x = new NewEntity();
        x.setName("anan");
        em.persist(x);
        em.getTransaction().commit();
        em.close();
        emf.close();   
    }        
}

最后是我的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="JavaApplication11PU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>javaapplication11.NewEntity</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/felek"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
  </persistence-unit>
</persistence>

1 个答案:

答案 0 :(得分:1)

这是整个代码吗?

因为堆栈跟踪的结尾指向JavaApplication11.java:29,这是你的main的标志,这很奇怪。