[英]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.