简体   繁体   中英

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:

<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

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. You have to invoke:

em.persist(cr);

before

t.commit();

Until you persist it, it's just a POJO in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM