gpt4 book ai didi

java - Netbeans 平台和 Jpa EclipseLink

转载 作者:行者123 更新时间:2023-12-02 12:02:55 28 4
gpt4 key购买 nike

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

[EL Info]: 2017-11-05 00:01:05.271--ServerSession(1276666395)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd [EL Info]: connection: 2017-11-05 00:01:05.451--ServerSession(1276666395)--file:/C:/Users/kB/Documents/NetBeansProjects/JavaApplication11/build/classes/_JavaApplication11PU login successful [EL Warning]: 2017-11-05 00:01:20.582--ServerSession(1276666395)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLException: Table 'room' already exists Error Code: 1050 Call: CREATE TABLE room (room_id INTEGER NOT NULL, room_name VARCHAR(255), PRIMARY KEY (room_id)) Query: DataModifyQuery(sql="CREATE TABLE room (room_id INTEGER NOT NULL, room_name VARCHAR(255), PRIMARY KEY (room_id))") [EL Warning]: 2017-11-05 00:01:35.687--ServerSession(1276666395)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLException: Table 'sequence' already exists Error Code: 1050 Call: CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT DECIMAL(38), PRIMARY KEY (SEQ_NAME)) Query: DataModifyQuery(sql="CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT DECIMAL(38), PRIMARY KEY (SEQ_NAME))") [EL Warning]: 2017-11-05 00:01:35.816--UnitOfWork(190868662)--java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number at 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) at 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) at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496) at javaapplication11.JavaApplication11.main(JavaApplication11.java:29) C:\Users\kB\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 31 seconds)

这是我的实体类

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>

最佳答案

这是完整的代码吗?

因为堆栈跟踪的末尾指向 JavaApplication11.java:29,这是 main 的 } 符号,这很奇怪。

关于java - Netbeans 平台和 Jpa EclipseLink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47115675/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com