2013-02-25 36 views
2

我正在使用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能告訴我該怎麼解決,這將是非常有益的警告。

+0

可能的重複http://stackoverflow.com/questions/10866263/spring-3-1-hibernate-4-1-jpa-entity-manager-factory-is-registered-twice/15052288#15052288。 – zagyi 2013-02-25 17:17:32

+0

這不完全相同的問題,但它幫助:) 我已經在我的applicationcontext和我的持久性XML文件中定義了實體管理器工廠...刪除了applicationContext中的一個,並解決了問題!謝謝 – Bart 2013-02-25 17:39:46

回答

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的電動勢= Persistence.createEntityManagerFactory( 「JpaPersistenceUnit」);

下一個:你可以通過這個獲得EntityManager類 @PersistenceContext protected EntityManager entityManager;

ps:對不起,我的英語很差, 希望對你有所幫助!

相關問題