0

I have a problem with create a table from @Entity class automatically. When I create table manually everything is ok. I can inject entityManager so on. I found many solution like this but doesn't work for me. My configuration: persistence.xml

    <persistence-unit name="entityManagerFactory"
    transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:jdbc/oracleTest</jta-data-source>
    <class>pl.example.was.test.entity.ResourceEntity</class>

    <properties>
        <property name="packagesToScan" value="pl.example.was.test.entity" />
        <property name="hibernate.archive.autodetection" value="class, hbm" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
        <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        <property name="show_sql" value="true" />
        <property name="format_sql" value="true" />
        <property name="hibernate.hbm2ddl.auto" value="true" />

        <property name="hibernate.transaction.factory_class"
            value="org.hibernate.transaction.CMTTransactionFactory" />
        <property name="hibernate.transaction.manager_lookup_class"
            value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
    </properties>
</persistence-unit>

part of Entity class

@Entity
 public class ResourceEntity implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String name;
// getters setters //

Inject EntityManager

@PersistenceContext
private EntityManager em;

I'm creating two test method

public void addResource(ResourceEntity selectedResource, Locale locale) {
    selectedResource.setLocale(locale.toString());
    try{
    Session session = em.unwrap(Session.class);
    Transaction tx = session.beginTransaction();
    session.save(selectedResource);
    session.flush();
    session.clear();
    tx.commit();
    session.close();
    }catch (Exception ex){
        ex.printStackTrace();
    }
}

and

public void addResource2(ResourceEntity selectedResource, Locale locale) {
    selectedResource.setLocale(locale.toString());
    em.persist(selectedResource);
}

I'm reading that I have to create Session object or EntityManager after that tables are creating automatically, but not in my case. My hibernate dependecies

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.2.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.2.7.Final</version>
    </dependency>

somebody have any idea?

Thanks for help

Community
  • 1
  • 1
luprogrammer
  • 155
  • 1
  • 3
  • 12
  • What is the exact problem? Do you get a stack trace when you run your code? If so, post the stack trace. – codedabbler Jun 25 '15 at 16:48
  • No I don't have stack trace. The application run normally, but table doesn't create in database. When I want save `ResourceEntity` object I have a information: `table or view does not exist` because table not been created. This information is clear but I don't know why. Hibernate should create table automatically – luprogrammer Jun 26 '15 at 06:06

1 Answers1

2

This one is problematic for sure:

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

You are asking Hibernate to drop the schema at the end of the Session. That's why you end up with no tables. What you probably want here is:

<property name="hibernate.hbm2ddl.auto" value="create" />

Moreover, true is not a valid value for hibernate.hbm2ddl.auto, so better delete this:

<property name="hibernate.hbm2ddl.auto" value="true" />

This has been already discussed here.

Community
  • 1
  • 1
mmalik
  • 185
  • 2
  • 11