简体   繁体   English

我没有收到任何错误,那么为什么我的数据不能持久保存到数据库中?

[英]I'm not getting any error, then why is not my data persisting into the database?

Greetings my folks I'm using JPA 2.0 and so far I'm not really getting any kind of error from anywhwere in the project, however the data is not persiting into the database at all, this is my persistence.xml (just the pu) file as it looks right now: 问候我的人们我正在使用JPA 2.0,到目前为止,我在项目中并未真正收到任何错误,但是数据根本没有渗透到数据库中,这是我的persistence.xml(仅是pu )文件的外观:

<persistence-unit name="auto-core-pu" transaction-type="RESOURCE_LOCAL">
        <description>Auto Core Persistence Unit</description>
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.auto.core.model.Cars</class>
        <class>com.auto.core.model.Client</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/auto_core"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="123456"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
    </persistence-unit>
</persistence>

This is my EntityManagerClient.class 这是我的EntityManagerClient.class

package com.auto.core.persistence;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import com.auto.core.model.Cars;

public class EntityManagerClient {
 public static void main(String[] args){
    EntityManager em = Persistence.createEntityManagerFactory("auto-core-pu").createEntityManager();
    EntityTransaction t = em.getTransaction();

    t.begin();

    Cars cr = new Cars();
    cr.setBrand("Toyota");
    cr.setModel("Camry");
    cr.setEngine("V6");
    cr.setDoors(5);
    cr.setType("Jeepeta");
    cr.setYear(123);
    cr.setDescription("Auto usado del año 123 D.C.");

    t.commit();


 }

 public static void getData(){
     EntityManager em = Persistence.createEntityManagerFactory("auto-core-pu").createEntityManager();
     EntityTransaction g = em.getTransaction();

     g.begin();

     Cars cr = em.createNamedQuery("Cars.FindbyId", Cars.class).setParameter("id", 1L).getSingleResult();
     System.out.println(cr.getBrand());

 }

}

and lass an example of how I did the annotations in other classes: 并举例说明我如何在其他类中进行注释:

@Entity
@Table(schema="auto_core" , name="client")
public class Client {

    @Id
    @GeneratedValue
    long id;
    @Column(name="name")
    String name;
    @Column(name="lastname")
    String lastname;
    @Column(name="email")
    String email;
    @Column(name="address")
    String address;
    @Column(name="address2")
    String address2;
    @Column(name="zip")
    String zip;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

Again, I'm not getting any kind of error, is just that I can't persist data, or query data that I put manually into the database, that's all. 再说一次,我没有得到任何错误,仅仅是我不能持久化数据,或者查询我手动放入数据库中的数据,仅此而已。

It's because you're getting the EntityManager, opening the transaction, and creating the object but you don't persist it. 这是因为您要获取EntityManager,打开事务并创建对象,但是您不能持久化它。 You have to invoke: 您必须调用:

em.persist(cr);

before 之前

t.commit();

Until you persist it, it's just a POJO in memory. 在您persist之前,它只是内存中的POJO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在运行文件时出现此错误? - Why I'm getting this error when I run my file? 我没有收到任何错误,但没有任何内容插入到我的数据库中 - I am not getting any error but nothing is getting inserted into my database 为什么我在输入中得到这样的null - Why i'm getting such null in my input 为什么当我尝试将一些数据发布到 firebase 数据库时,网络不可用,正在休眠? - Why I'm getting network unavailable, sleeping when I try to post some data to a firebase database? 为什么我在文件中获取垃圾数据? - Why I'm getting junk data in the File? 我的程序出现错误 - I'm getting error in my program 不知道为什么我会收到NullPointerException错误 - Not sure why I'm getting a NullPointerException error 为什么我在 android 监视器中收到此错误? - why i'm getting this error in android monitor? 调用getChoice()方法时出现错误,我不知道为什么 - I'm getting an error here when I call my getChoice() method, and I don't know why (Java)(SQL)我正在尝试检查数据库中的用户输入以登录,但我却发现参数类型不匹配错误? - (Java)(SQL) I'm trying to check a user input against my database to login but I'm getting and arguement type mismatch error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM