如何使用我的persistence.xml创建我的EntityManagerFactory的实例?

我正在使用Spring,Hibernate和JPA创建后端应用程序。 目前应用程序测试通过,但我收到警告: 警告:HHH000436:实体管理器工厂名称(JpaPersistenceUnit)已注册。如何使用我的persistence.xml创建我的EntityManagerFactory的实例?

我认为这样做的原因是我在persistence.xml中定义了我的JpaPersistenceUnit,并且我也在我的dao类中创建了一个。如果是这种情况,我需要找到一种方法从persistence.xml中获取我的JpaPersistenceUnit,而不必再次创建它。但我不知道怎么...

这是我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?> 

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

<persistence-unit name="JpaPersistenceUnit"

transaction-type="RESOURCE_LOCAL">

<provider>org.hibernate.ejb.HibernatePersistence</provider>

<properties>

<property name="hibernate.archive.autodetection" value="class, hbm"/>

<property name="hibernate.show_sql" value="true"/>

<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>

<property name="hibernate.connection.password" value="groepD"/>

<property name="hibernate.connection.url" value="jdbc:mysql://localhost/groepd"/>

<property name="hibernate.connection.username" value="groepD"/>

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

<property name="hibernate.hbm2ddl.auto" value="update"/>

</properties>

</persistence-unit>

</persistence>

这是我的泛型DAO类:

public interface GenericDao<E, ID extends Serializable> { 

EntityManagerFactory emf = Persistence.createEntityManagerFactory("JpaPersistenceUnit");

public void add(E entity);

public void remove(E entity);

public void update(E entity);

public E findById(ID id);

public List<E> findAll();

}

这是具体的DAO类:

public interface TripDao extends GenericDao<Trip,Integer> { 

}

这是dao类的实现:

@Repository 

public class TripDaoImpl implements TripDao {

protected EntityManager entityManager;

public TripDaoImpl() {

entityManager = emf.createEntityManager();

}

@Override

@Transactional

public void add(Trip entity) {

entityManager.getTransaction().begin();

entityManager.persist(entity);

entityManager.getTransaction().commit();

}

....

}

这是实体:

@Entity 

@Table(name = "T_TRIP")

public class Trip {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Integer id;

@NotNull

private String name;

@ManyToMany(cascade = CascadeType.ALL)

@JoinTable(name="T_TRIP_ADMINS",

joinColumns={@JoinColumn(name="tripId")},

inverseJoinColumns={@JoinColumn(name="userId")})

private Set<User> admins;

@ManyToMany(cascade = CascadeType.ALL)

@JoinTable(name="T_TRIP_PARTICIPANT",

joinColumns={@JoinColumn(name="tripId")},

inverseJoinColumns={@JoinColumn(name="userId")})

private Set<User> invitedUsers;

@NotNull

private Boolean privateTrip;

@NotNull

private Boolean published;

@Enumerated(EnumType.STRING)

private TripType type;

@NotNull

private Integer nrDays;

@NotNull

private Integer nrHours;

@OneToMany(cascade = CascadeType.ALL)

@JoinColumn(name = "tripId")

private Set<Stop> stops;

public Trip(){

initLists();

}

private void initLists(){

this.admins = new HashSet<User>();

this.invitedUsers = new HashSet<User>();

this.stops = new HashSet<Stop>();

}

public void addStop(Stop stop) {

stops.add(stop);

}

public boolean removeStop(Stop stop) {

if (stops.size() > 1 && stops.contains(stop)) {

stops.remove(stop);

return true;

} else {

return false;

}

}

...More getters and setters...

}

如果someboby能告诉我该怎么解决,这将是非常有益的警告。

回答:

第一:(在你的applicationContext.xml)

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 

<property name="dataSource" ref="dataSource"/>

<property name="persistenceXmlLocation"

value="classpath:persistence.xml">

</property>

<property name="jpaVendorAdapter">

<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">

<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />

</bean>

</property>

<property name="loadTimeWeaver"> <!-- 运行时植入 -->

<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />

</property>

</bean>

下:(更新您的代码) 删除:EntityManagerFactory的电动势= Persistence.createEntityManagerFactory( “JpaPersistenceUnit”);

下一个:你可以通过这个获得EntityManager类 @PersistenceContext protected EntityManager entityManager;

ps:对不起,我的英语很差, 希望对你有所帮助!

以上是 如何使用我的persistence.xml创建我的EntityManagerFactory的实例? 的全部内容, 来源链接: utcz.com/qa/262679.html

回到顶部