我遇到了Maven / Spring项目的问题。我正在尝试从MySQL Workbench应用数据库,但没有成功。
以下是我的文件:
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="shakira" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="myPass" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql//localhost:3306/shakira" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
HibernateTest.java
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.packt.webstore.domain.Product;
public class HibernateTest {
private static EntityManagerFactory entityManagerFactory;
public static void main(String[] args) {
Product product = new Product();
product.setId(1);
product.setName("Name");
try {
entityManagerFactory = Persistence.createEntityManagerFactory("shakira");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist( product );
entityManager.getTransaction().commit();
System.out.println("successfull");
entityManager.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.3 http://maven.apache.org/xsd/maven-4.0.3.xsd">
<modelVersion>4.0.3</modelVersion>
<groupId>com.packt</groupId>
<artifactId>webstore</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.1.4.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.10</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.1.Final</version>
</dependency>
</dependencies>
<resources>
<resource>
<directory>src/main/resources/META-INF</directory>
<targetPath>META-INF</targetPath>
<includes>
<include>persistence.xml</include>
</includes>
</resource>
</resources>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
错误
> 2017-02-14 23:34:43WARN HibernatePersistence:58 - HHH015016:
> Encountered a deprecated javax.persistence.spi.PersistenceProvider
> [org.hibernate.ejb.HibernatePersistence]; use
> [org.hibernate.jpa.HibernatePersistenceProvider] instead. 2017-02-14
> 23:34:43WARN HibernatePersistence:58 - HHH015016: Encountered a
> deprecated javax.persistence.spi.PersistenceProvider
> [org.hibernate.ejb.HibernatePersistence]; use
> [org.hibernate.jpa.HibernatePersistenceProvider] instead. 2017-02-14
> 23:34:43INFO LogHelper:31 - HHH000204: Processing PersistenceUnitInfo
> [ name: shakira ...] 2017-02-14 23:34:43INFO Version:45 -
> HHH000412: Hibernate Core {5.2.7.Final} 2017-02-14 23:34:43INFO
> Environment:213 - HHH000206: hibernate.properties not found 2017-02-14
> 23:34:43INFO Version:66 - HCANN000001: Hibernate Commons Annotations
> {5.0.1.Final} 2017-02-14 23:34:43WARN pooling:70 - HHH10001002: Using
> Hibernate built-in connection pool (not for production use!)
> 2017-02-14 23:34:43INFO pooling:126 - HHH10001005: using driver
> [com.mysql.jdbc.Driver] at URL [jdbc:mysql//localhost:3306/shakira]
> 2017-02-14 23:34:43INFO pooling:135 - HHH10001001: Connection
> properties: {user=root, password=****} 2017-02-14 23:34:43INFO
> pooling:140 - HHH10001003: Autocommit mode: false 2017-02-14
> 23:34:43INFO DriverManagerConnectionProviderImpl:41 - HHH000115:
> Hibernate connection pool size: 20 (min=1)
> org.hibernate.service.spi.ServiceException: Unable to create requested
> service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
> at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
> at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
> at
> org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
> at
> org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
> at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
> at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
> at
> org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
> at
> org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
> at
> org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:858)
> at
> org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885)
> at
> org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
> at
> org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
> at
> javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
> at
> javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
> at HibernateTest.main(HibernateTest.java:19) Caused by:
> org.hibernate.HibernateException: Unable to make JDBC Connection
> [jdbc:mysql//localhost:3306/shakira] at
> org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:60)
> at
> org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:123)
> at
> org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:42)
> at
> org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:20)
> at
> org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:161)
> at
> org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:109)
> at
> org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:72)
> at
> org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
> at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
> at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
> at
> org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
> at
> org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
> at
> org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
> at
> org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
> at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)
> ... 15 more
Product.java
package com.packt.webstore.domain;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
//import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.web.multipart.MultipartFile;
//import javax.xml.bind.annotation.XmlRootElement;
//import javax.xml.bind.annotation.XmlTransient;
import com.packt.webstore.validator.Category;
import com.packt.webstore.validator.ProductId;
//@XmlRootElement
@Entity
//@Table(name = "Produkty")
public class Product {
@Id
@Column(name="id_produkt")
private long id;
@Pattern(regexp="P[0-9]+", message="{Pattern.Product.productId.validation}")
@ProductId
@Column(name="produkt_id_sklepowe")
private String productId;
@Size(min=4, max=50, message="{Size.Product.name.validation}")
@Column(name="nazwa")
private String name;
@Min(value=0, message="Min.Product.unitPrice.validation}")
@Digits(integer=8, fraction=2, message="{Digits.Product.unitPrice.validation}")
@NotNull(message="{NotNull.Product.unitPrice.validation}")
@Column(name="cena")
private BigDecimal unitPrice;
@Column(name="opis")
private String description;
@Column(name="producent")
private String manufacturer;
@Category(message = "{Category.Product.category.validation}")
@Column(name="kategoria")
private String category;
@Column(name="ilosc_dostepnych_sztuk")
private long unitsInStock;
@Transient
private long unitsInOrder;
@Transient
private boolean discontinued;
@Transient
private String condition;
//@JsonIgnore
@Transient
private MultipartFile productImage;
@Transient
private MultipartFile productPdf;
public Product(){
super();
}
public Product(String productId, String name, BigDecimal unitPrice){
this.productId=productId;
this.name=name;
this.unitPrice=unitPrice;
}
@Override
public boolean equals(Object obj){
if(this == obj)
return true;
if(obj==null)
return false;
if(getClass()!=obj.getClass())
return false;
Product other = (Product)obj;
if(productId==null){
if(other.productId != null)
return false;
}else if(!productId.equals(other.productId))
return false;
return true;
}
@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime*result + ((productId ==null)?0:productId.hashCode());
return result;
}
@Override
public String toString(){
return "Produkt [productId="+productId +", nazwa=" + name+"]";
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(BigDecimal unitPrice) {
this.unitPrice = unitPrice;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public long getUnitsInStock() {
return unitsInStock;
}
public void setUnitsInStock(long unitsInStock) {
this.unitsInStock = unitsInStock;
}
public long getUnitsInOrder() {
return unitsInOrder;
}
public void setUnitsInOrder(long unitsInOrder) {
this.unitsInOrder = unitsInOrder;
}
public boolean isDiscontinued() {
return discontinued;
}
public void setDiscontinued(boolean discontinued) {
this.discontinued = discontinued;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
//@XmlTransient
public MultipartFile getProductImage() {
return productImage;
}
public void setProductImage(MultipartFile productImage) {
this.productImage = productImage;
}
public MultipartFile getProductPdf() {
return productPdf;
}
public void setProductPdf(MultipartFile productPdf) {
this.productPdf = productPdf;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
我找到了一些解决方案,但没有一个有效。这是我尝试过的列表:
我还把地址放在互联网浏览器中,我收到了一个内部文件:
N
5.7.17-log # ;#80&X ˙÷! ˙ IY
me)q^* mysql_native_password ! ˙„#08S01Got packets out of order
也许这就是线索?
提前感谢您的帮助!
我可能通过添加hibernate.properties而不是persistence.xml来解决它(我删除了标签内persistence.xml中的所有标签)
这是 hibernate.properties
文件hibernate.connection.autocommit=true
hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=create
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url= jdbc:mysql://localhost:3306/shakira
hibernate.connection.username=root
hibernate.connection.password=szczypi)rek13
hibernate.connection.pool_size=2
答案 0 :(得分:0)
尝试使用HibernatePersistenceProvider
:
org.hibernate.jpa.HibernatePersistenceProvider
...
PersistenceProvider provider = new HibernatePersistenceProvider();
entityManagerFactory = provider.createEntityManagerFactory(
"shakira", null);
就在侧面,第二个参数是Map of properties
,您要覆盖或添加persistence.xml
中定义的参数。在您的情况下,只需传递null
值即可。
<强>更新强>
您可能会考虑的另一件事是在提交之前添加flush()
:
entityManager.getTransaction().begin();
entityManager.persist( product );
entityManager.flush();
entityManager.getTransaction().commit();
System.out.println("successfull");
entityManager.close();