[英]How to add a service module in wildfly-swarm with .war packaging?
[英]Java Wildfly-Swarm: How to use JDBC with SQL Server (T-SQL)
我正在使用Wildfly-Swarm在microsoft azure上構建一個java web應用程序。
我的配置:
的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;" +
"user=user@myDB;" +
"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時,一切都正常運行,但如果我嘗試調用Rest API,我會收到以下錯誤:
com.microsoft.sqlserver.jdbc.SQLServerException:關鍵字“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 ......有沒有人知道如何解決這個問題?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.