PersistenceUnit [h2]的预部署因UUID而失败?类:从类名转换为类时找不到[uuid]

时间:2018-09-28 18:04:40

标签: jpa eclipselink persistence h2 uuid

当我尝试持久化我的实体时,出现以下异常: 线程“主”中的异常javax.persistence.PersistenceException:异常[EclipseLink-28019](Eclipse Persistence Services-2.7.3.v20180807-4be1041):org.eclipse.persistence.exceptions.EntityManagerSetupException

运行时异常:

异常描述:PersistenceUnit [h2]的部署失败。关闭此PersistenceUnit的所有工厂。

内部异常:异常[EclipseLink-0](Eclipse Persistence Services-2.7.3.v20180807-4be1041):org.eclipse.persistence.exceptions.IntegrityException 异常[EclipseLink-7198](Eclipse Persistence Services-2.7.3.v20180807-4be1041):org.eclipse.persistence.exceptions.ValidationException 描述符异常:

异常描述:类:从类名转换为类时找不到[uuid]。

内部异常:java.lang.ClassNotFoundException:uuid

实体

@Entity
@UuidGenerator(name = "uuid")
@Converter(name = "uuidConverter", converterClass = UUIDConverter.class)
@Table(name = "ELECTRIC_METERS")
@NamedQuery(name = "ElectricMeters.findElectricMetersByNote", 
        query = "SELECT e FROM ElectricMeters e WHERE e.note = :note")
public class ElectricMeters implements Serializable{

    @Id
    @GeneratedValue(generator = "uuid", strategy = IDENTITY)
    @Convert("uuidConverter")
    @Column (name = "ID")
    private UUID id;

    @Column(name = "ADD_YARD_LIGHTING")
    private boolean addYardLighting;

    @Column(name = "NOTE")
    private String note;

    public ElectricMeters() {
    }
//getters setters
}

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="h2" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <!-- Converter -->
        <class>com.art.forestbucha.util.UUIDConverter</class>

        <class>com.art.forestbucha.entity.ElectricMeters</class>

        <properties>
          <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
          <property name="javax.persistence.jdbc.url" value="jdbc:h2:file://home/artem/NetBeansProjects/ForestBuchaBackEnd/src/main/resources/db/bucha"/>

          <property name="javax.persistence.jdbc.user" value="sa"/>
          <property name="javax.persistence.jdbc.password" value=""/>
        </properties>
      </persistence-unit>
    </persistence>

转换器

import java.util.UUID;
import org.eclipse.persistence.internal.helper.DatabaseField; 
import org.eclipse.persistence.mappings.DatabaseMapping; 
import org.eclipse.persistence.mappings.DirectCollectionMapping; 
import org.eclipse.persistence.mappings.converters.Converter; 
import org.eclipse.persistence.sessions.Session;


public class UUIDConverter implements Converter{

    @Override
    public Object convertObjectValueToDataValue(Object objectValue, 
            Session session) { 
        return (UUID) objectValue;
    } 

    @Override 
    public UUID convertDataValueToObjectValue(Object dataValue, 
            Session session) { 
        return (UUID) dataValue; 
    } 

    @Override 
    public boolean isMutable() { 
        return true; 
    } 

    @Override 
    public void initialize(DatabaseMapping mapping, Session session) { 
        final DatabaseField field; 
        if (mapping instanceof DirectCollectionMapping) { 
            // handle @ElementCollection... 
            field = ((DirectCollectionMapping) mapping).getDirectField(); 
        } else { 
            field = mapping.getField(); 
        } 

        field.setSqlType(java.sql.Types.OTHER); 
        field.setTypeName("uuid"); 
        field.setColumnDefinition("UUID"); 
    }
}

创建数据库:

CREATE TABLE PUBLIC.ELECTRIC_METERS
(ID UUID NOT NULL PRIMARY KEY,
ADD_YARD_LIGHTING BOOLEAN DEFAULT false NOT NULL,
NOTE VARCHAR(255));

0 个答案:

没有答案