gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:26:02 25 4
gpt4 key购买 nike

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

我认为这是因为我在 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...
}

如果有人能告诉我如何修复警告,那将非常有帮助。

最佳答案

首先:(在你的 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 emf = Persistence.createEntityManagerFactory("JpaPersistenceUnit");

下一步:你可以通过这个获取类 EntityManager@PersistenceContext protected EntityManager 实体管理器;

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

关于java - 如何使用我的 persistence.xml 创建我的 EntityManagerFactory 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15072415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com