我真的需要一些关于hibernate的帮助,我搜索所有问题,但它没有用。
我的问题很奇怪,至少我是这么认为的。在我的项目中,我想使用实体类,hibernate xml映射文件(* .hbm.xml)和hibernate配置文件(hibernate.cfg.xml)来创建mysql数据库中的表。奇怪的是我只能创建一个表,并且可以成功创建其他表。
好的,这些是我的项目代码,我认为可能会使用这些信息。
cart.java
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* @author Ning
*
*/
public class Cart implements Serializable{
private Integer id;
private User user;
private Map<Integer, Integer> shopItems = new HashMap<>();
public Cart() {}
public Cart(User user, Map<Integer, Integer> shopItems) {
super();
this.user = user;
this.shopItems = shopItems;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Map<Integer, Integer> getShopItems() {
return shopItems;
}
public void setShopItems(Map<Integer, Integer> shopItems) {
this.shopItems = shopItems;
}
@Override
public String toString() {
return "Cart [id=" + id + ", user=" + user + ", shopItems=" + shopItems + "]";
}
}
<!------cart.hbm.xml----->
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-4-30 9:23:54 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.eshop.design.model.Cart" table="CART">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<many-to-one name="user" class="com.eshop.design.model.User" fetch="join">
<column name="USER" />
</many-to-one>
<map name="shopItems">
<key column="CART_ID"></key>
<index column="PRODUCT_ID" type="java.lang.Integer"></index>
<element column="NUMBER" type="java.lang.Integer"></element>
</map>
</class>
</hibernate-mapping>
Order.java
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class Order implements Serializable{
private Integer id;
private User user;
private Map<Integer, Integer> shopItems = new HashMap<>();
private Date date;
private Status status;
private String remark;
public Order() {}
public Order(User user, Map<Integer, Integer> shopItems, Date date, Status status, String remark) {
super();
this.user = user;
this.shopItems = shopItems;
this.date = date;
this.status = status;
this.remark = remark;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Map<Integer, Integer> getShopItems() {
return shopItems;
}
public void setShopItems(Map<Integer, Integer> shopItems) {
this.shopItems = shopItems;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Override
public String toString() {
return "Order [id=" + id + ", user=" + user + ", shopItems=" + shopItems + ", date=" + date + ", status="
+ status + ", remark=" + remark + "]";
}
}
<!-------order.hbm.xml----->
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-4-30 9:23:54 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.eshop.design.model.Order" table="ORDER">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<many-to-one name="user" class="com.eshop.design.model.User" fetch="join">
<column name="USER" />
</many-to-one>
<map name="shopItems">
<key column="ORDER_ID"></key>
<index column="PRODUCT_ID" type="java.lang.Integer"></index>
<element column="NUMBER" type="java.lang.Integer"></element>
</map>
<property name="date" type="java.util.Date">
<column name="DATE" />
</property>
<many-to-one name="status" class="com.eshop.design.model.Status" fetch="join">
<column name="STATUS" />
</many-to-one>
<property name="remark" type="java.lang.String">
<column name="REMARK" />
</property>
</class>
</hibernate-mapping>
<!-----user.java------>
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
public class User implements Serializable{
private Integer id;
private String login;
private String pass;
private String name;
private String phone;
private String address;
private Double assets;
// Constructors
/** default constructor */
public User() {}
/** full constructor */
public User( String login, String pass, String name, String phone, String address, Double assets) {
super();
this.login = login;
this.pass = pass;
this.name = name;
this.phone = phone;
this.address = address;
this.assets = assets;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Double getAssets() {
return assets;
}
public void setAssets(Double assets) {
this.assets = assets;
}
@Override
public String toString() {
return "User [id=" + id + ", login=" + login + ", pass=" + pass + ", name=" + name + ", phone=" + phone
+ ", address=" + address + ", assets=" + assets + "]";
}
}
<!-----user.hbm.xml---->
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-4-30 9:23:54 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.eshop.design.model.User" table="USER">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="login" type="java.lang.String">
<column name="LOGIN" />
</property>
<property name="pass" type="java.lang.String">
<column name="PASS" />
</property>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="phone" type="java.lang.String">
<column name="PHONE" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
<property name="assets" type="java.lang.Double">
<column name="ASSETS" />
</property>
</class>
</hibernate-mapping>
<!-- I config the data source and session factory in the spring's configuration file applicationContext.xml-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="minPoolSize" value="${minPoolSize}" />
<property name="maxPoolSize" value="${maxPoolSize}" />
<property name="initialPoolSize" value="${initialPoolSize}" />
</bean>
<!-- SessionFactory config -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/eshop/design/model/*.hbm.xml"></property>
</bean>
<!----hibernate.cfg.xml--->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
这是我第一次在这里提问,有些格式可能不能令人满意。
提前致谢并忘记我的英语不好。