我有一个应该在mysql中创建表的实现。这仅用于测试代码的功能。
但它已经过去了,我不知道为什么,我的课程看起来像这样:
@ManagedBean
@ApplicationScoped
public class Shop {
private List<Artikel> sortiment = new ArrayList<Artikel>();
private static Shop instance = new Shop();
public Shop() {
Artikel a = new Artikel();
a.setId(1);
a.setName("Samsung");
EntityManagerFactory factory = Persistence.createEntityManagerFactory("onlineshop");
EntityManager manager = factory.createEntityManager();
manager.getTransaction().begin();
manager.persist(a);
manager.getTransaction().commit();
}
}
这个类是我的Managed Bean类。
这个是我的持久单位:
<?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="onlineshop" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/onlineshop"/>
<property name="javax.persistence.jdbc.user" value="benjamin"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.password" value="start123"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
我的实体级看起来像这样:
@Entity
public class Artikel implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
正如您所看到的,它只是一小段代码,但它不适合我。 包含所有必需的库,也包括mysql.jar文件。
修改
这是我收录的罐子