**This is the pom.xml file**
` <dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.24.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies> `
**This is my hibernate.cfg.xml file**
` <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mycart</property>
<property name="connection.username" >root</property>
<property name="connection.password">@DIKDIKHATK</property>
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="hibernate.hdm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<mapping class="com.resumeproject.indicart.entities.User"/>
<mapping class="com.resumeproject.indicart.entities.Category"/>
<mapping class="com.resumeproject.indicart.entities.Product"/>
</session-factory>
</hibernate-configuration>`
**This is the user entity**
`package com.resumeproject.indicart.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(length=10,name="user_id")
private int userId;
@Column(length=100,name="user_name")
private String userNamme;
@Column(length=100,name="user_email")
private String userEmail;
@Column(length=100,name="user_password")
private String userPassword;
@Column(length=12,name="user_phone")
private String userPhone;
@Column(length=1500,name="user_pic")
private String userPic;
@Column(length=1500,name="user__address")
private String userAddress;
public User(int userId, String userNamme, String userEmail, String userPassword,String userPhone,String userPic, String userAddress) {
this.userId = userId;
this.userNamme = userNamme;
this.userEmail = userEmail;
this.userPassword = userPassword;
this.userPhone = userPhone;
this.userPic = userPic;
this.userAddress = userAddress;
}
public User(String userNamme, String userEmail, String userPassword, String userPhone, String userPic, String userAddress) {
this.userNamme = userNamme;
this.userEmail = userEmail;
this.userPassword = userPassword;
this.userPhone = userPhone;
this.userPic = userPic;
this.userAddress = userAddress;
}
public User() {
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserNamme() {
return userNamme;
}
public void setUserNamme(String userNamme) {
this.userNamme = userNamme;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserPhone() {
return userPhone;
}
public void setUserPhone(String userPhone) {
this.userPhone = userPhone;
}
public String getUserPic() {
return userPic;
}
public void setUserPic(String userPic) {
this.userPic = userPic;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
@Override
public String toString() {
return "User{" + "userId=" + userId + ", userNamme=" + userNamme + ", userEmail=" + userEmail + ", userPassword=" + userPassword + ", userPhone=" + userPhone + ", userPic=" + userPic + ", userAddress=" + userAddress + '}';
}
字符串
}`
**This is my Category entity**
`package com.resumeproject.indicart.entities;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Category {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int categoryId;
private String categoryTitle;
private String categoryDescription;
@OneToMany(mappedBy="category")
private List<Product> products=new ArrayList<>();
public Category(int categoryId, String categoryTitle, String categoryDescription) {
this.categoryId = categoryId;
this.categoryTitle = categoryTitle;
this.categoryDescription = categoryDescription;
}
public Category() {
}
public Category(String categoryTitle, String categoryDescription,List<Product> products) {
this.categoryTitle = categoryTitle;
this.categoryDescription = categoryDescription;
this.products=products;
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public String getCategoryTitle() {
return categoryTitle;
}
public void setCategoryTitle(String categoryTitle) {
this.categoryTitle = categoryTitle;
}
public String getCategoryDescription() {
return categoryDescription;
}
public void setCategoryDescription(String categoryDescription) {
this.categoryDescription = categoryDescription;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
@Override
public String toString() {
return "Category{" + "categoryId=" + categoryId + ", categoryTitle=" + categoryTitle + ", categoryDescription=" + categoryDescription + '}';
}
型
}'这是我的产品实体
`package com.resumeproject.indicart.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int pId;
private String pMame;
@Column(length=3000)
private String pDesc;
private String pPhoto;
private int pPrice;
private int pDicsount;
private int pQuantity;
@ManyToOne
private Category category;
public Product() {
}
public Product(String pMame, String pDesc, String pPhoto, int pPrice, int pDicsount, int pQuantity,Category category) {
this.pMame = pMame;
this.pDesc = pDesc;
this.pPhoto = pPhoto;
this.pPrice = pPrice;
this.pDicsount = pDicsount;
this.pQuantity = pQuantity;
this.category=category;
}
public int getpId() {
return pId;
}
public void setpId(int pId) {
this.pId = pId;
}
public String getpMame() {
return pMame;
}
public void setpMame(String pMame) {
this.pMame = pMame;
}
public String getpDesc() {
return pDesc;
}
public void setpDesc(String pDesc) {
this.pDesc = pDesc;
}
public String getpPhoto() {
return pPhoto;
}
public void setpPhoto(String pPhoto) {
this.pPhoto = pPhoto;
}
public int getpPrice() {
return pPrice;
}
public void setpPrice(int pPrice) {
this.pPrice = pPrice;
}
public int getpDicsount() {
return pDicsount;
}
public void setpDicsount(int pDicsount) {
this.pDicsount = pDicsount;
}
public int getpQuantity() {
return pQuantity;
}
public void setpQuantity(int pQuantity) {
this.pQuantity = pQuantity;
}
@Override
public String toString() {
return "Product{" + "pId=" + pId + ", pMame=" + pMame + ", pDesc=" + pDesc + ", pPhoto=" + pPhoto + ", pPrice=" + pPrice + ", pDicsount=" + pDicsount + ", pQuantity=" + pQuantity + '}';
}
型
}'的一个例子
project directory overview
这是Java应用程序的代码。“我试图更改Hibernate配置文件中的一些依赖项和一些配置,但无法实现我对数据库的期望。请告诉我我错过了什么。
我在控制台上找不到任何关于Mysql查询的信息。
1条答案
按热度按时间bvjxkvbb1#
表不会自己创建。
你需要在数据库中创建它们,除非你使用Sping Boot 或其他可以自动创建表的东西?