2

I am creating a backend application using Spring, Hibernate and JPA. Currently the application test pass but I get a warning: WARN: HHH000436: Entity manager factory name (JpaPersistenceUnit) is already registered.

I think the reason for this is that I defined my JpaPersistenceUnit in the persistence.xml and I also create one in my dao class. If this is the case I need to find a way to get my JpaPersistenceUnit from the persistence.xml without creating it (again). But I don't know how...

This is my 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>

This is my generic dao class:

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();
}

This is the specific dao class:

public interface TripDao  extends GenericDao<Trip,Integer> {
}

And this is an implementation of the dao class:

@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();
}

....
}

This is the entity:

@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...
}

If someboby could tell me how to to fix the warning that would be very helpfull.

Bart
  • 363
  • 1
  • 3
  • 6
  • Possible duplicate of http://stackoverflow.com/questions/10866263/spring-3-1-hibernate-4-1-jpa-entity-manager-factory-is-registered-twice/15052288#15052288. – zagyi Feb 25 '13 at 17:17
  • It was not exactly the same problem but it helped :) I had defined an Entity manager factory in my applicationcontext and my persistence XML files... Deleted the one in applicationContext and that fixed the problem! thx – Bart Feb 25 '13 at 17:39

1 Answers1

2

first: (in you 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> 

next:(update you code) remove :EntityManagerFactory emf = Persistence.createEntityManagerFactory("JpaPersistenceUnit");

next: you can get the class EntityManager by this @PersistenceContext protected EntityManager entityManager;

ps:sorry my english is so poor, wish helpfull for you!

ouyang
  • 21
  • 3