Mapping classes HIBERNATE

2

Well, I have a question about an exercise that was passed in the facul recently. I have to map classes with JPA, Hibernate and I'm having a bit of trouble in mapping the relationships of those classes.

  • The problem is this, the exercise is about a Football Game where we have the classes Player, Time, Technician, Judge, Game.

  • In the relationship, Player has a Team and the Team has several Players. So good!

  • How I mapped this example, and would like you to help me because I can not generate my DB with this mapping.

    Here is the Player class where I refer to his Team. @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name="time_Cod", insertable=true,updatable=true) @Fetch(FetchMode.JOIN) @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE) private Time time;

    And my Team that contains several Players.

    @OneToMany(mappedBy="Time",fetch = FetchType.LAZY) @Cascade(CascadeType.ALL) private ArrayList<Jogador> jogador;

  • There are other relationships, but if I put it here you would end up doing my kkkk activity so only that little help would be wonderful.

  • If you need more information, please let me know, and thank you in advance!

  • EDIT- 1 Error: set 06, 2016 12:45:00 PM org.hibernate.ejb.HibernatePersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead. set 06, 2016 12:45:00 PM org.hibernate.ejb.HibernatePersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead. set 06, 2016 12:45:00 PM org.hibernate.ejb.HibernatePersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead. set 06, 2016 12:45:00 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation INFO: HHH000204: Processing PersistenceUnitInfo [ name: conexaoDB ...] set 06, 2016 12:45:00 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.3.8.Final} set 06, 2016 12:45:00 PM org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found set 06, 2016 12:45:00 PM org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist set 06, 2016 12:45:00 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final} set 06, 2016 12:45:00 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!) set 06, 2016 12:45:00 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/db_testeAtvdAp02] set 06, 2016 12:45:00 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000046: Connection properties: {user=root, password=****} set 06, 2016 12:45:00 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000006: Autocommit mode: false set 06, 2016 12:45:00 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000115: Hibernate connection pool size: 20 (min=1) set 06, 2016 12:45:01 PM org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect Erro no cadastro: [PersistenceUnit: conexaoDB] Unable to build Hibernate SessionFactory

    EDIT 2- Persistence File:

    <persistence version="2.0" 
    xmlns="http://java.sun.com/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="conexaoDB" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name = "javax.persistence.jdbc.user" value="root" />
            <property name = "javax.persistence.jdbc.password" value="" />
            <property name = "javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/db_testeAtvdAp02" />
        </properties>
    </persistence-unit>
    

    EDIT 3 - Code

    public static void main(String[] args) {
    
    
        try {
    
    
            EntityManagerFactory factory = Persistence.createEntityManagerFactory("conexaoDB");
            factory.close();
    
    
          System.out.println("Gerado");     
        } catch (Exception e) {
          System.out.println("Não Gerado: "+e.getMessage());
        }
    
    }
    
        
    asked by anonymous 06.09.2016 / 16:54

    1 answer

    1

    Well, in your persistence.xml you need to add your entities in tags<class> </class> , you just have to publish the fully qualified name of class ex: <class>br.com.financas.modelo.Conta</class> no persistence.xml .

    In your case it will give a warning saying that your provider is obsolete, just update the configuration of persistence.xml , followed by provider : Ex:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.1" 
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    </persistence>'
    
        
    06.09.2016 / 19:32