2

我正在使用 Spring、Hibernate 和 JPA 创建一个后端应用程序。目前应用程序测试通过但我收到警告:WARN: HHH000436: Entity manager factory name (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...
}

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

4

1 回答 1

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> 

下一个:(更新您的代码)删除:EntityManagerFactory emf = Persistence.createEntityManagerFactory("JpaPersistenceUnit");

next:可以通过这个@PersistenceContext protected EntityManager entityManager 获取类EntityManager;

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

于 2013-02-26T03:26:35.003 回答