2016-05-24 11 views
1

Ich benutze Wildfly-Swarm, um eine Java-Webanwendung auf Microsoft Azure aufzubauen.Java Wildfly-Swarm: Verwendung von JDBC mit SQL Server (T-SQL)

Meine Konfiguration:

  • JDK 8.0
  • Wildfly-Swarm 1.0.0.Beta8
  • Maven 3.2
  • JDBC 4.2 Microsoft SQL Server

pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 

    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.example.azure.swarm</groupId> 
    <artifactId>AzureSwarmTest</artifactId> 
    <name>Wildfly Swarm Example</name> 
    <version>1.0.0-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <properties> 
    <version.sqlserver>4.0</version.sqlserver> 
    <version.wildfly.swarm>1.0.0.Beta8</version.wildfly.swarm> 
    <maven.compiler.source>1.8</maven.compiler.source> 
    <maven.compiler.target>1.8</maven.compiler.target> 
    <failOnMissingWebXml>false</failOnMissingWebXml> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <dependencyManagement> 
    <dependencies> 
     <dependency> 
     <groupId>org.wildfly.swarm</groupId> 
     <artifactId>bom</artifactId> 
     <version>${version.wildfly.swarm}</version> 
     <scope>import</scope> 
     <type>pom</type> 
     </dependency> 
    </dependencies> 
    </dependencyManagement> 

    <build> 
    <finalName>AzureSwarmTest</finalName> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.2</version> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
     </plugin> 
     <plugin> 
     <groupId>org.wildfly.swarm</groupId> 
     <artifactId>wildfly-swarm-plugin</artifactId> 
     <version>${version.wildfly.swarm}</version> 
     <configuration> 
      <mainClass>com.example.Main</mainClass> 
     </configuration> 
     <executions> 
      <execution> 
      <goals> 
       <goal>package</goal> 
      </goals> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 

    <dependencies> 
    <!-- Java EE 7 dependency --> 
    <dependency> 
     <groupId>javax</groupId> 
     <artifactId>javaee-api</artifactId> 
     <version>7.0</version> 
     <scope>provided</scope> 
    </dependency> 
    <!-- Wildfly Swarm Fractions --> 
    <dependency> 
     <groupId>org.wildfly.swarm</groupId> 
     <artifactId>ejb</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.wildfly.swarm</groupId> 
     <artifactId>jpa</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.wildfly.swarm</groupId> 
     <artifactId>jaxrs-cdi</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.wildfly.swarm</groupId> 
     <artifactId>swagger-webapp</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.wildfly.swarm</groupId> 
     <artifactId>datasources</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>com.microsoft.sqlserver</groupId> 
     <artifactId>sqljdbc42</artifactId> 
     <version>4.2</version> 
     <scope>compile</scope> 
    </dependency> 
    </dependencies> 
</project> 

persistence.xml

<?xml version="1.0" encoding="UTF-8"?> 
<persistence 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     version="2.1" 
     xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> 
    <persistence-unit name="MyPU" transaction-type="JTA"> 

     <class>com.example.model.User</class> 

     <properties> 
      <property name="hibernate.default_schema" value="MY_SCHEMA"/> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect"/> 
      <property name="hibernate.show_sql" value="true"/> 
      <property name="hibernate.format_sql" value="true"/> 
     </properties> 
    </persistence-unit> 
</persistence> 

... void main (String ... args)

public static void main(String[]args) throws Exception{ 

    Container container = new Container(); 

    container.fraction(datasourceWithSqlServer()); 

    container.fraction(new JPAFraction() 
     .inhibitDefaultDatasource() 
     .defaultDatasource("jboss/datasources/MyDS")); 

    container.start(); 

    JAXRSArchive appDeployment = ShrinkWrap.create(JAXRSArchive.class); 
    appDeployment.addClasses(User.class); 
    appDeployment.addAsLibrary(container.createDefaultDeployment()); 
    appDeployment.addAllDependencies(); 

    container.deploy(appDeployment); 

} 

private static DatasourcesFraction datasourceWithSqlServer(){ 
    return new DatasourcesFraction() 
      .jdbcDriver("sqlserver", (d) -> { 
       d.driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
       d.xaDatasourceClass("com.microsoft.sqlserver.jdbc.SQLServerXADataSource"); 
       d.driverModuleName("com.microsoft"); 
      }) 
      .dataSource("myDS", (ds) -> { 
       ds.driverName("sqlserver"); 
       ds.connectionUrl("jdbc:sqlserver://myDB.database.windows.net:1433;" + 
         "database=myDB;" + 
         "[email protected];" + 
         "password=password;" + 
         "encrypt=true;" + 
         "trustServerCertificate=true;" + 
         "hostNameInCertificate=*.database.windows.net;" + 
         "loginTimeout=30;" 
       ); 
      }); 


} 

... Simple Modell

@Entity 
@Table(name = "USER") 
@NamedQueries({ 
     @NamedQuery(name = User.GET_ALL, query = "SELECT u FROM User u") 
}) 
@XmlRootElement 
public class User implements Serializable { 

    /** The Constant serialVersionUID. */ 
    private static final long serialVersionUID = -5895315599733726081L; 

    public static final String GET_ALL = "User.GET_ALL_USER"; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    @Column(name = "FIRST_NAME") 
    private String firstName; 

    @Column(name = "LAST_NAME") 
    private String lastName; 

    protected User(){} 

    public String getFirstName() { 
     return firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public Long getId() { 
     return id; 
    } 

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

    @Override 
    public String toString() { 
     return "User{" + 
       "firstName='" + firstName + '\'' + 
       ", lastName='" + lastName + '\'' + 
       '}'; 
    } 

    public void update(User user){ 
     this.firstName = user.getFirstName(); 
     this.lastName = user.getLastName(); 
    } 

} 

... Service

@Stateless 
public class UserService { 

    @PersistenceContext 
    EntityManager em; 

    public List<User> getAll(){ 
     return em.createNamedQuery(User.GET_ALL, User.class).getResultList(); 
    } 
} 

... REST-API

> @ApplicationScoped 
@Path("/user") 
public class HelloWorldEndpoint { 

    @EJB 
    protected UserService service; 

    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    public List<User> doGet() { 
     return service.getAll(); 

    } 
} 

Wenn ich laufen Maven und führen Sie die * -swarm.jar -datei alles richtig läuft, aber wenn ich versuchen, den Rest API ich die folgende Fehlermeldung zu nennen:

com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'USER'

ich das bekommen SQL sysout folgende:

Hibernate: 
    select 
     user0_.id as id1_0_, 
     user0_.FIRST_NAME as FIRST_NA2_0_, 
     user0_.LAST_NAME as LAST_NAM3_0_ 
    from 
     MY_SCHEMA.USER user0 

Dieser Fehler kann durch die Verwendung NamedNativeQuery gelöst werden, aber ich will NamedQuery verwenden. Es scheint, dass Hibernate.dialect JP-QL nicht in T-SQL übersetzen kann ... Hat jemand eine Idee, wie man das Problem lösen kann?

Antwort

0

Es scheint, dass Sie ein reserviertes Schlüsselwort "USER" für Ihre Tabelle verwenden, wie in diesem T-SQL reservierten Schlüsselwort documentation beschrieben.

Die einfachste Lösung wäre wahrscheinlich, Ihre USER-Tabelle zu etwas anderem wie USERS umzubenennen, noch mehr als reservierte Schlüsselwörter verwenden, wird nicht empfohlen.

Siehe auch this question.

Verwandte Themen