私は、wildfly-swarmを使用して、Microsoft AzureでJava Webアプリケーションを構築しています。Java Wildfly-Swarm:SQL ServerでJDBCを使用する方法(T-SQL)
私の構成:
- JDK 8.0
- Wildfly-スウォーム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>
...無効メイン(文字列...引数)
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;"
);
});
}
...簡単なモデル
@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();
}
}
...サービス
@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();
}
}
私はMavenを実行し、* -swarm.jar -fileを実行すると、すべてが正常に動作しますが、私は残りのAPIを呼び出そうと、私は次のエラーを取得する:
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'USER'
私が手SQL sysoutに続いて:
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
このエラーはNamedNativeQueryを使用して解決できますが、NamedQueryを使用します。 hibernate.dialectはJP-QLをT-SQLに「翻訳」できないようです...誰かが問題の解決方法を手掛かりにしていますか?