-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Hibernate 5.1 Table annotation schema parameter not working
PostPosted: Fri Nov 17, 2017 1:26 am 
Newbie

Joined: Fri Nov 17, 2017 1:23 am
Posts: 7
I am trying to migrate my app running on hibernate 4.3 (wildfly) to 5.1.2 of wildfly 11.

One show stopper is that the "schema" parameter inside the @Table annotation does not seem to work anymore.

For example I have the following Entities:

Code:
@Entity
@Table(name = "ObjectA", schema = "schemaA")

public class A {





     @JoinColumn(name = "schemaB_ID")

     public B getPropertyType() {

        return b;

     }

}

@Entity
@Table(name = "ObjectB", schema = "schemaB")

public class B {



}



The two entities are in different schemas sitting on the same mysql server.

In persistence.xml my default entityManagerFactory is schemaA.

However when I use jpa query:

A fetch Join B, the application tries to find Table "ObjectB" on schemaA instead of trying to find it on SchemaB. So the @Table(name = "ObjectB", schema = "schemaB") is ignored?



The same query works fine in wildfly 8.2 with hibernate 4.3.

Not sure what I have to change in wildfly 11 with hibernate 5.1 and not sure what is breaking this, JPA? Hibernate? or Wildfly?


Top
 Profile  
 
 Post subject: Re: Hibernate 5.1 Table annotation schema parameter not working
PostPosted: Fri Nov 17, 2017 2:43 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You need to replicate it using a dedicated unit test, which you can do via the JPA test case template. However, you need to do it for 5.2.12, as this is the latest development branch.

Once you have that, you should open a Jira issue and upload the test case as well.

If the issue is replicated and we fix it, it might be backported to 5.1.

Thanks


Top
 Profile  
 
 Post subject: Re: Hibernate 5.1 Table annotation schema parameter not working
PostPosted: Thu Nov 23, 2017 12:31 am 
Newbie

Joined: Fri Nov 17, 2017 1:23 am
Posts: 7
ok, I created a test case based on your JPA test cases. I created two entities: Event and Location.
An event could have many locations.
I added schema attribute:
@Table(name = "Event", schema = "db1") in Event class
and
@Table(name = "Location", schema = "db2") in Location class
I also update the persistence units and create the second unit for schema "db2"
When I ran, it give me errors:
Caused by: org.h2.jdbc.JdbcSQLException: Schema "DB1" not found; SQL statement:

drop table db1.Event if exists [90079-176]

I also tried
@Table(name = "db1.Event") got same error.

It could be I did not configure something right. I need to figure out how to upload the zip file which contains the persistence.xml and test cases.

Thanks.


Top
 Profile  
 
 Post subject: Re: Hibernate 5.1 Table annotation schema parameter not working
PostPosted: Thu Nov 23, 2017 12:36 am 
Newbie

Joined: Fri Nov 17, 2017 1:23 am
Posts: 7
The jpa test case code:
Code:
package org.hibernate.bugs;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
* This template demonstrates how to develop a test case for Hibernate ORM, using the Java Persistence API.
*/
public class JPAUnitTestCase {

   private EntityManagerFactory entityManagerFactory;
   private EntityManagerFactory entityManagerFactory2;

   @Before
   public void init() {

      entityManagerFactory = Persistence.createEntityManagerFactory( "templatePU" );
      entityManagerFactory2 = Persistence.createEntityManagerFactory( "templatePU2" );
   }

   @After
   public void destroy() {
      entityManagerFactory2.close();
      entityManagerFactory.close();
   }

   // Entities are auto-discovered, so just add them anywhere on class-path
   // Add your tests, using standard JUnit.
   @Test
   public void twoSchemaJoinTest() throws Exception {
      EntityManager entityManager = entityManagerFactory.createEntityManager();
      try {
         entityManager.getTransaction().begin();

         Location location1 = new Location("city1");
         Location location2 = new Location("city2");
         entityManager.persist(location1);
         entityManager.persist(location2);
         Set<Location> locations = new HashSet<>();
         locations.add(location1);
         locations.add(location2);
         Event event = new Event(new Date());
         event.setLocations(locations);
         entityManager.persist(event);
         entityManager.getTransaction().commit();

         entityManager.getTransaction().begin();
         String queryString = "Select distinct t from Event t join fetch t.locations";
         Query query = entityManager.createQuery(queryString);
         Event event1 = (Event) query.getSingleResult();
         System.out.println(event1.getId());

         //assertEquals(event.getCreatedOn(), dbEvent.getCreatedOn());
         entityManager.getTransaction().commit();
      }
      catch(Exception e){
         e.printStackTrace();
      }
      finally{
         entityManager.close();
      }

   }

   private void createLocations(){
      EntityManager entityManager2 = entityManagerFactory2.createEntityManager();
      entityManager2.getTransaction().begin();

      Location location1 = new Location("city1");
      Location location2 = new Location("city2");


      entityManager2.persist( location1 );
      entityManager2.persist( location2 );


      entityManager2.getTransaction().commit();
      entityManager2.close();
   }
}

















Top
 Profile  
 
 Post subject: Re: Hibernate 5.1 Table annotation schema parameter not working
PostPosted: Thu Nov 23, 2017 12:38 am 
Newbie

Joined: Fri Nov 17, 2017 1:23 am
Posts: 7
The Event and Locations entities:
Code:
package org.hibernate.bugs;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "Event", schema = "db1")
public class Event {
    private Long id;
    @Temporal(TemporalType.TIMESTAMP )
    private Date createdOn;
    private Set<Location> locations;

    public Event() {
    }
    public Event(Date createdOn) {
        this.createdOn = createdOn;
    }


    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH}, targetEntity = Location.class)
    public Set<Location> getLocations() {
        return locations;
    }

    public Date getCreatedOn() {
        return createdOn;
    }

    public void setLocations(Set<Location> locations) {
        this.locations=locations;
    }

    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    public void setId(Long id) {

        this.id = id;
    }



}


package org.hibernate.bugs;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "Location", schema = "db2")
public class Location {
    private Long id;
    String name;

    public Location() {
    }

    public Location(String name) {
        this.name = name;
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    @Column(name = "NAME", unique = true, nullable = false, length = 128)
    public String getName(){
        return this.name;
    }


    public void setName(String name){
        this.name =  name;
    }

    public void setId(Long id) {
        this.id = id;
    }
}























Top
 Profile  
 
 Post subject: Re: Hibernate 5.1 Table annotation schema parameter not working
PostPosted: Thu Nov 23, 2017 12:39 am 
Newbie

Joined: Fri Nov 17, 2017 1:23 am
Posts: 7
The persistence.xml:
Code:
<persistence 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="2.1">
<!--<persistence 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 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"-->
             <!--version="2.0">-->

    <persistence-unit name="templatePU" transaction-type="RESOURCE_LOCAL">

        <description>Hibernate test case template Persistence Unit</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>org.hibernate.bugs.Event</class>

        <exclude-unlisted-classes>false</exclude-unlisted-classes>


        <properties>
            <property name="hibernate.archive.autodetection" value="class, hbm"/>

            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1"/>
            <property name="hibernate.connection.username" value="sa"/>

            <property name="hibernate.connection.pool_size" value="5"/>

            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

            <property name="hibernate.max_fetch_depth" value="5"/>

            <property name="hibernate.cache.region_prefix" value="hibernate.test"/>
            <property name="hibernate.cache.region.factory_class"
                      value="org.hibernate.testing.cache.CachingRegionFactory"/>

            <!--NOTE: hibernate.jdbc.batch_versioned_data should be set to false when testing with Oracle-->
            <property name="hibernate.jdbc.batch_versioned_data" value="true"/>

            <property name="javax.persistence.validation.mode" value="NONE"/>
            <property name="hibernate.service.allow_crawling" value="false"/>
            <property name="hibernate.session.events.log" value="true"/>
        </properties>

    </persistence-unit>
    <persistence-unit name="templatePU2" transaction-type="RESOURCE_LOCAL">

        <description>Hibernate test case template Persistence Unit</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <class>org.hibernate.bugs.Location</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <properties>
            <property name="hibernate.archive.autodetection" value="class, hbm"/>

            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:h2:mem:db2;DB_CLOSE_DELAY=-1"/>
            <property name="hibernate.connection.username" value="sa"/>

            <property name="hibernate.connection.pool_size" value="5"/>

            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

            <property name="hibernate.max_fetch_depth" value="5"/>

            <property name="hibernate.cache.region_prefix" value="hibernate.test"/>
            <property name="hibernate.cache.region.factory_class"
                      value="org.hibernate.testing.cache.CachingRegionFactory"/>

            <!--NOTE: hibernate.jdbc.batch_versioned_data should be set to false when testing with Oracle-->
            <property name="hibernate.jdbc.batch_versioned_data" value="true"/>

            <property name="javax.persistence.validation.mode" value="NONE"/>
            <property name="hibernate.service.allow_crawling" value="false"/>
            <property name="hibernate.session.events.log" value="true"/>
        </properties>

    </persistence-unit>
</persistence>
















Top
 Profile  
 
 Post subject: Re: Hibernate 5.1 Table annotation schema parameter not working
PostPosted: Thu Nov 23, 2017 2:45 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Since you already have the test case, you need to do two things:

1. Go to Hibernate Jira and create a new issue.
2. Create an archive of the replicating test case project and attach it to the Jira issue you justs created.


Top
 Profile  
 
 Post subject: Re: Hibernate 5.1 Table annotation schema parameter not working
PostPosted: Fri Dec 01, 2017 11:48 am 
Newbie

Joined: Fri Nov 17, 2017 1:23 am
Posts: 7
I opened a jira last week
https://hibernate.atlassian.net/browse/HHH-12126
But it has not been assigned yet. Could you get it assigned?
Thanks a lot.


Top
 Profile  
 
 Post subject: Re: Hibernate 5.1 Table annotation schema parameter not working
PostPosted: Fri Dec 01, 2017 12:19 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
If you are really in a hurry with this issue, I suggest you try to fix it and provide a Pull Request. Integrating Pull Requests has a higher priority than fixing an issue.

Otherwise, you'd have to wait for someone from the team to fix it, but the majority of developers are working on 5.3 and 6.0, and there is a gathering next week as well.

So, in the spirit of open-source software development, let's all try to make Hibernate better.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.