如何解决org.hibernate.internal.util.config.ConfigurationException: 找不到 cfg.xml 资源 [hibernate.cfg.xml]
我正在尝试完成以下项目,以使用 hibernate 和 java 注释而不使用 hbm 文件更新具有一对多和多对一关系的数据库。一个用户可以拥有多台笔记本电脑。我的项目在 NetBeans、eclipse 上运行良好。如何在 Web IDE 中抛出上述错误。我在代码中遗漏了什么。
用户实体-:
@Entity
public class User {
@Id
private int id;
private String first_name;
private String last_name;
private String email;
@OneToMany(targetEntity = Laptop.class,mappedBy = "User",cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private List<Laptop> laptop = new ArrayList<Laptop>();
public User(int id,String first_name,String last_name,String email,List<Laptop> laptop) {
super();
this.id = id;
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
this.laptop = laptop;
}
public List<Laptop> getLaptop() {
return laptop;
}
public void setLaptop(List<Laptop> laptop) {
this.laptop = laptop;
}
public User(int id,String email) {
super();
this.id = id;
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
}
@Override
public String toString() {
return "Student [id=" + id + ",first_name=" + first_name + ",last_name=" + last_name + ",email=" + email
+ ",laptop=" + laptop + "]";
}
public User(String first_name,String email) {
super();
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
笔记本电脑实体-:
@Entity
public class Laptop {
@Id
private int laptop_id;
private String laptop_name;
@ManyToOne
@JoinColumn(name = "user_id",referencedColumnName = "id") //,nullable = false
private User User;
public User getUser() {
return User;
}
public Laptop() {
super();
}
public void setUser(User user) {
this.User = user;
}
public int getId() {
return laptop_id;
}
public void setId(int id) {
this.laptop_id = id;
}
public String getLaptop_name() {
return laptop_name;
}
public void setLaptop_name(String laptop_name) {
this.laptop_name = laptop_name;
}
public Laptop(int id,String laptop_name) {
super();
this.laptop_id = id;
this.laptop_name = laptop_name;
}
@Override
public String toString() {
return "Laptop [id=" + laptop_id + ",laptop_name=" + laptop_name + ",user=" + User + "]";
}
}
我的测试类 -:
@Transactional
public class user {
private Session session= null;
@Before
public void before() {
Configuration config = new Configuration().configure().addAnnotatedClass(User.class)
.addAnnotatedClass(Laptop.class);
SessionFactory factory = config.buildSessionFactory();
session = factory.openSession();
}
@org.junit.Test
public void test1() throws Exception{
//LaptopRepository lr;
User user = new User(100,"Escofe","Labro","Escofe@Labro");
Laptop laptop = new Laptop(2,"MACbookPro");
laptop.setUser(user);
user.getLaptop().add(laptop);
Transaction tx = session.beginTransaction();
session.save(user);
session.save(laptop);
tx.commit();
assertEquals("MACbookPro",laptop.getLaptop_name());
}
@org.junit.Test
public void test2() throws Exception{
//LaptopRepository lr;
User user = new User(100,"Anushka","Shetty","anushka@gmali.in");
Laptop laptop = new Laptop(1,"DELLXPS");
laptop.setUser(user);
user.getLaptop().add(laptop);
Transaction tx = session.beginTransaction();
session.save(user);
session.save(laptop);
tx.commit();
assertEquals("Anushka",user.getFirst_name());
assertEquals("DELLXPS",laptop.getLaptop_name());
}
}
在提供任何输入之前请注意以下几点 -:
1> 我的 hibernate.cfg.xml 在 src 文件夹中
2>我确实尝试过配置(“hibernate.cfg.xml”)甚至完整路径
3> 我的项目在 IDE 中运行良好,如 NetBeans 和 eclipse 在 web ide 中不起作用
如果我遗漏了什么,请帮助我
解决方法
因为您已经为您的问题尝试了很多解决方案,但这些都不适用于您的项目。因此,我正在分享您可以做的事情。请检查这是否可以帮助...
1> 将您的配置文件移动到您的主项目文件夹,然后提供配置文件的路径(“hibernate.cfg.xml”)如果在错误消息中抛出错误检查它采用的默认路径(错误消息将显示您路径以及错误)。如果不采用默认路径,请尝试提供完整路径。也试试下面的参考路径为...
File f = new File(""hibernate.cfg.xml"");
Configuration config = new Configuration().configure(f).addAnnotatedClass(User.class)
.addAnnotatedClass(Laptop.class);
有时它有效:)
2> 尝试在没有 hibernate.cfg.xml 文件的情况下使用 java 配置检查数据库准备情况。你可以尝试如下..(如果使用任何其他数据库,下面基于 mysql chnage 详细信息)
Configuration cfg = new Configuration()
.addAnnotatedClass(your clas)
.addAnnotatedClass(your clas)
.setProperty("hibernate.dialect","org.hibernate.dialect.MySQL5Dialect")
.setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver")
.setProperty("hibernate.connection.url","jdbc:mysql://localhost:3306/yourdb")
.setProperty("hibernate.connection.username","your username")
.setProperty("hibernate.connection.password","your password")
.setProperty("hibernate.hbm2ddl","create")
.setProperty("hibernate.show_sql","true");
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
在这里,您至少可以了解您的问题。
希望这有助于解决您的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。