我正在使用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...
}
如果有些人可以告诉我如何修复那些非常有帮助的警告。
答案 0 :(得分:2)
首先:(在你的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>
下一篇:(更新你的代码) remove:EntityManagerFactory emf = Persistence.createEntityManagerFactory(“JpaPersistenceUnit”);
next:你可以通过这个获得类EntityManager @PersistenceContext protected EntityManager entityManager;
ps:对不起,我的英语太差了, 希望对你有所帮助!