1

create tables from Java in database on Microsoft SQL Server 2012. All tables are created, except one table. I'm using JPA and there is my persistence.xml :

<?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">
  <persistence-unit name="teknikPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/teknikNDataSource</jta-data-source>
    <class>com.royken.entities.Bloc</class>
    <class>com.royken.entities.Elements</class>
    <class>com.royken.entities.Organes</class>
    <class>com.royken.entities.SousOrganes</class>
    <class>com.royken.entities.Utilisateurs</class>
    <class>com.royken.entities.Zone</class>
    <class>com.royken.entities.Reponse</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
      <property name="eclipselink.logging.level" value="OFF"/>
      <property name="eclipselink.cache.shared.default" value="false"/>
      <property name="eclipselink.query-results-cache" value="false"/>
      <!--            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.transaction.jta.platform" value="org.hibernate.engine.transaction.jta.platform.internal.SunOneJtaPlatform" />
            <property name="hibernate.transaction.factory_class" value="org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.classloading.use_current_tccl_as_parent" value="false"/>-->
      <!--<property name="javax.persistence.schema-generation.database.action" value="create"/> -->
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
  </persistence-unit>
</persistence>

This is how I define my classes :

@Entity
@XmlRootElement(name = "elements")
@Table(name = "ELEMENTS")
@XmlAccessorType(XmlAccessType.FIELD)
public class Elements implements Serializable {

    private static final long serialVersionUID = 1L;
    @OneToMany(mappedBy = "elements")
    private List<Reponse> reponses;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;

    @Version
    @Column(name = "VERSION")
    private int version;

    @Column(name = "NOM")
    private String nom;

    @Column(columnDefinition = "tinyint(1) default true", name = "HASBORNS")
    private boolean hasBorns;

    @Column(columnDefinition = "tinyint(1) default true", name = "CRITERIAALPHA")
    private boolean criteriaAlpha;

}

I have defined 7 tables like that, but only 6 tables are created, Elements tables is not created. When I change the datasource by using a mysql database (without changing any part of code), all my tables are well created. What can be the issue ? The image bellow shows the result in SQL server, Elements table is not present. enter image description here

Royken
  • 170
  • 1
  • 1
  • 9

2 Answers2

0

In your persistence.xml Use :

<property name="eclipselink.deploy-on-startup" value="true" />

In your code, you may use:

import javax.ejb.Stateless;
import entity.userEntity;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

Look for EntityManager

 @PersistenceContext
            private EntityManager entityManager;

and then use it like :

Query query = entityManager.createQuery("SELECT e FROM Elements e WHERE e.id= :idValue");
            query.setParameter("idValue", 1);
            Elements elements = null;
            try {
                elements = (Elements) query.getSingleResult();
} catch (NoResultException ex) {
                ex.printStackTrace();
            }

You may refer to this

If that too doesn't help look here

Surajit Biswas
  • 779
  • 7
  • 25
  • Hello @Surajit, I tried but it is not working. 06 tables are automatically created and one is not. While on MySQL all the tables are automatically created with the same code. – Royken Mar 27 '18 at 17:07
0

I found the solution to my problem. Entity table was not created because SQL Server does not accept true as default value for hasborns and criteriaalpha. Also, it does not now the size to allocate to tinyint type. So it throws an error during table creation. To solve this issue, I replaced:

@Column(columnDefinition = "tinyint(1) default true", name = "HASBORNS")
private boolean hasBorns;

@Column(columnDefinition = "tinyint(1) default true", name = "CRITERIAALPHA")
private boolean criteriaAlpha;

with:

@Column(columnDefinition = "BIT default 1", name = "HASBORNS", length = 1)
private boolean hasBorns ;

@Column(columnDefinition = "BIT default 1", name = "CRITERIAALPHA", length = 1)
private boolean criteriaAlpha ;

And it worked

Royken
  • 170
  • 1
  • 1
  • 9