gpt4 book ai didi

java - 映射异常 : Type can not be determined for java. util.Set

转载 作者:行者123 更新时间:2023-12-01 11:10:36 24 4
gpt4 key购买 nike

运行以下程序时出现错误

org.hibernate.MappingException: Could not determine type for: java.util.Set, for columns: [org.hibernate.mapping.Column(ListOfAddress)]

我添加了import javax.persistence.ElementCollection;但我遇到了同样的错误

有人可以建议我任何选择吗?

HibernateTest.java

package src.com.hibernate.main;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.mapping.*;
import src.com.hibernate.Address;
import src.com.hibernate.UserDetails;

public class HibernateTest {

public static void main(String[] args)
{
UserDetails user = new UserDetails();
user.setUserName("Surendar");

Address addr = new Address();
addr.setStreet("Abith colony");
addr.setCity("Chennai");
addr.setState("TamilNAdu");
addr.setPincode("600015");

Address addr1= new Address();
addr1.setStreet("Anna Salai");
addr1.setCity("Chennaimain");
addr1.setState("TN");
addr1.setPincode("600033");

user.getListOfAddress().add(addr);
user.getListOfAddress().add(addr1);

SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
session.close();

}

}

UserDetails.java

      package src.com.hibernate;


import java.util.HashSet;
import java.util.Set;

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="UserTABLEaddress")
public class UserDetails
{

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
private String userName;

@SuppressWarnings("rawtypes")
@ElementCollection
private Set<Address> ListOfAddress = new HashSet();

public Set<Address> getListOfAddress() {
return ListOfAddress;
}
public void setListOfAddress(Set<Address> listOfAddress) {
ListOfAddress = listOfAddress;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}

地址.java

      package src.com.hibernate;
import javax.persistence.Embeddable;

@Embeddable
public class Address
{

private String street;

private String city;

private String state;

private String pincode;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}

}

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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="src.com.hibernate.UserDetails"/>
</session-factory>
</hibernate-configuration>

最佳答案

我认为你正在混淆概念。您尝试在用于多对一关系的类中使用@Embeddable注释。这就是你的错误的原因。

如果要将ListOfAddress用作元素集合,则必须将Address也定义为实体并正确配置关系,例如:

...
@ElementCollection
@CollectionTable(name="address", joinColumns=@JoinColumn(name="userId"))
@Column(name="addresses")
private Set<Address> ListOfAddress = new HashSet<Address>();
...

在地址类中

@Entity
@Table(name="address")
public class Address
...

互联网上有很多例子。

关于java - 映射异常 : Type can not be determined for java. util.Set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32436205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com