Mapping classes HIBERNATE

Asked

Viewed 265 times

2

Guys, I have a question about an exercise that was passed on campus recently. I have to map classes with JPA, Hibernate and I’m having a little difficulty in mapping the relationships of these classes.

  1. The problem is this, the exercise is about a Football Game where we have the classes Player, Team, Coach, Judge, Game.

  2. In the relationship would be Player has a Team and the Team has several Players. So far so good!

  3. How I mapped this example, and I would like you to help me because I’m not being able to generate my DB with this mapping.

    This 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 Multi-player Team.

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

  4. There are other relationships, but, if I put here you would end up doing my kkkk activity then only this little help would be wonderful.

  5. If you need more information, please let me know, and thank you!

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- Persintencia archive:

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

}
  • Send stacktrace of the generated error and try to change the mappedBy="Time" for mappedBy="time"

  • Okay mark, I made the change you said and executed, but I still continue with this mistake

  • ok, this problem is in relation to your database connection configuration, more specifically your persistence drive, post your configuration file (persistence.xml) and the connection factory code @user54273

  • Okay Marcos, I just edited!

  • in his persistence.xml missing vc add your entities in tags<class> </class>(Player, Team, Coach, Judge, Game) the ones that contain in your code. I don’t understand why you declare factory use in a method main, but you don’t have to close it! erase: factory.close(); and modify <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/> for <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> in the persistence.xml

  • Marcos, in relation to Entitymanagerfactory I put there because of the slides that were passed, in relation to adding the entities in my persistence.xml I did not know this very thank you, I was going to be insisting all my life on something that I was not even aware of, now I go in search of this!

  • ok, you just have to publish the Fully Qualified name of the ex class: <class>br.com.financas.modelo.Conta</class> don’t forget to say if it worked, because I don’t think that’s all.

  • All right, I’ll try and get back to you.

  • Agora estou recebendo um erro:&#xA;WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.

  • can believe, but this is just a warning that your provider is obsolete (did your code work despite this?) use <?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"> version of persistence.xml and add the <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> This has to do with the summer of Hibernate you are using. Note that the version is JPA2.1

  • Now, Marcos, fight, you saved my ass :) !

  • Seriously, man? Just to help other people, I’ll post this as an answer and you close the question as closed, and mark the answer as useful, blz?

  • Blz then, you can post !

Show 8 more comments

1 answer

1


Well, in your persistence.xml missing vc add your entities in tags<class> </class>, you just have to publish the Fully Qualified name ex class: <class>br.com.financas.modelo.Conta</class> in the persistence.xml.

In your case you will give a Warning saying that his preview is obsolete, just update the configuration of the persistence.xml, with the seguite preview: 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>`

Browser other questions tagged

You are not signed in. Login or sign up in order to post.