本文主要是介绍persistent.xml,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="jpaPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="hibernate.connection.username" value="root"></property>
<property name="hibernate.connection.password" value="root"></property>
<property name="hibernate.show_sql" value="true"></property>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"></property>
<property name="hibernate.current_session_context_class" value="thread"></property>
</properties>
</persistence-unit>
<!--
<persistence-unit name="jpaPU" transaction-type="JTA">
<jta-data-source>jndi_mysql</jta-data-source>
</persistence-unit>
-->
</persistence>
package user1;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;@Entity
@Table(name="user_table")
public class User1 {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)private int id;@Column(name="name",length=10)private String name;@Column(name="pass")private String pass;public User1(){}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPass() {return pass;}public void setPass(String pass) {this.pass = pass;}}
package user1;import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;public class test {public static void main(String args[]){EntityManagerFactory emf=Persistence.createEntityManagerFactory("jpaPU");final EntityManager em = emf.createEntityManager();User1 user1= new User1();user1.setName("test");user1.setPass("pass2ff");try {em.getTransaction().begin();em.persist(user1); //添加//修改User1 user2=em.find(User1.class, 1);user2.setName("what_chang");em.getTransaction().commit();} catch (RuntimeException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{em.close();}}}
这篇关于persistent.xml的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!