有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何使用Java持久性API创建简单的SQL Server插入

  • SQL Server连接:正常

  • 对于MS SQL jar:OK

  • 对于Hibernate ORM jar:OK

  • 没有插入到数据库中

  • 我运行主类时出错

  • 是“hibernate.cfg.xml” 需要吗

私人的。java是模型类。 JavaJpaTestImpl。java是主类

个人。爪哇

package JpaTestPackage;

import java.io.Serializable;
import javax.persistence.*;

@Entity
@NamedQuery(name="Personel.findAll", query="SELECT p FROM Personel p")

public class Personel implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="Id")
    private int id;

    @Column(name="Personel_Adi")
    private String personel_Adi; 

    @Column(name="Personel_Soyadi")
    private String personel_Soyadi;

    public Personel() {
    }

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

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

    public String getPersonel_Adi() {
        return this.personel_Adi;
    }

    public void setPersonel_Adi(String personel_Adi) {
        this.personel_Adi = personel_Adi;
    }

    public String getPersonel_Soyadi() {
        return this.personel_Soyadi;
    }

    public void setPersonel_Soyadi(String personel_Soyadi) {
        this.personel_Soyadi = personel_Soyadi;
    }

}

JavaJpaTestImpl。爪哇

public class JavaJpaTestImpl {

    public static void main(String [] args)
    {
        EntityManager em;
        EntityManagerFactory emf;
        Personel personel=new Personel();
        personel.setPersonel_Adi("Kerem");
        personel.setPersonel_Soyadi("GUL");
        emf=Persistence.createEntityManagerFactory("JpaTest");
        em=emf.createEntityManager();
        EntityTransaction et= em.getTransaction();
        et.begin();
        em.persist(personel);
        et.commit();
        em.close();
        emf.close();


    }

}

坚持。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="JpaTest" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>JpaTestPackage.Personel</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost:1433;databaseName=aaaa"/>
            <property name="javax.persistence.jdbc.user" value="bbbb"/>
            <property name="javax.persistence.jdbc.password" value="cccc"/>
            <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
        </properties>
    </persistence-unit>
</persistence>

例外情况:

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/jandex/IndexView
    at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:51)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:129)
    at org.hibernate.ejb.HibernatePersistence.getEntityManagerFactoryBuilderOrNull(HibernatePersistence.java:93)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:86)
    at org.hibernate.ejb.HibernatePersistence.getEntityManagerFactoryBuilderOrNull(HibernatePersistence.java:101)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:67)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at JpaTestPackage.JavaJpaTestImpl.main(JavaJpaTestImpl.java:17)
Caused by: java.lang.ClassNotFoundException: org.jboss.jandex.IndexView
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

共 (3) 个答案

  1. # 1 楼答案

    jpa snapshot

    坚持。xml文件和其他jar文件显示在快照中

  2. # 2 楼答案

    将jandex jar库添加到类路径

  3. # 3 楼答案

    当我添加jandex jar时,它出现了以下错误

    log4j:WARN No appenders could be found for logger (org.jboss.logging).
    log4j:WARN Please initialize the log4j system properly.
    Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named JpaTest
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
        at JpaTestPackage.JavaJpaTestImpl.main(JavaJpaTestImpl.java:17)