你好,我剛開始學習休眠。請糾正我在哪裏做錯了。我想用一個使用hibernate註釋的連接表在兩個表之間建立一對多的關係。Hibernate註解執行內部聯接
create table assembly
(
assembly_id serial primary key,
number text,
user_id int
);
create table assembly_properties
(
property_id serial primary key,
property_name text,
property_type text
);
create table assembly_properties_mapping
(
mapping_id serial primary key,
assembly_id int,
property_id int,
property_value text,
CONSTRAINT FK_assembly_id FOREIGN KEY (assembly_id) REFERENCES assembly(assembly_id),
CONSTRAINT FK_property_id FOREIGN KEY (property_id) REFERENCES assembly_properties(property_id)
);
我已經在postgres sql數據庫中創建了這三個表。下面是我Assembly.class
package com.development.wrapper;
@Entity
@Table(name = "assembly")
public class Assembly {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "assembly_id")
private int assembly_id;
@Column(name = "number")
private String number;
@Column(name ="UserID")
private int userId;
@Column
@ElementCollection(targetClass = AssemblyProperties.class)
private Set<AssemblyProperties> assembly_properties;
public int getAssembly_id() {
return assembly_id;
}
public void setAssembly_id(int assembly_id) {
this.assembly_id = assembly_id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
@OneToMany(targetEntity = AssemblyProperties.class, cascade = CascadeType.ALL)
@JoinTable(name = "assembly_properties_mapping", joinColumns = { @JoinColumn(name = "assembly_id") }, inverseJoinColumns = { @JoinColumn(name = "property_id") })
public Set<AssemblyProperties> getAssembly_properties() {
return assembly_properties;
}
public void setAssembly_properties(Set<AssemblyProperties> assembly_properties) {
this.assembly_properties = assembly_properties;
}
}
下面是AssemblyProperties.class 包com.development.wrapper;
@Entity
@Table(name = "assembly_properties")
public class AssemblyProperties {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "property_id")
private int property_id;
@Column(name = "property_name")
private String property_name;
@Column(name = "property_type")
private String property_type;
public int getProperty_id() {
return property_id;
}
public void setProperty_id(int property_id) {
this.property_id = property_id;
}
public String getProperty_name() {
return property_name;
}
public void setProperty_name(String property_name) {
this.property_name = property_name;
}
public String getProperty_type() {
return property_type;
}
public void setProperty_type(String property_type) {
this.property_type = property_type;
}
}
當我試圖給出在數據庫表中加載數據低於我越來越無法創建SessionFactory的object.org.hibernate.MappingException錯誤:無法確定類型:com.development.wrapper。 AssemblyProperties,在表:Assembly_assembly_properties,爲列:[org.hibernate.mapping.Column(assembly_properties) 異常線程 「main」 java.lang.ExceptionInInitializerError
下面的代碼我試圖運行
public class Test
{
SessionFactory factory;
public Test() throws Exception
{
try
{
factory = new AnnotationConfiguration().configure().
addPackage("com.development.wrapper"). //add package if used.
addAnnotatedClass(Assembly.class).buildSessionFactory();
}
catch (Throwable ex)
{
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public Integer addClass(Assembly assembly)
{
Session session = factory.openSession();
Transaction tx = null;
Integer assemblyid = null;
try
{
tx = session.beginTransaction();
assemblyid = (Integer) session.save(assembly);
System.out.println(assemblyid);
tx.commit();
}
catch (HibernateException e)
{
if (tx != null)
tx.rollback();
e.printStackTrace();
}
finally
{
session.close();
}
return assemblyid;
}
public static void main(String[] args) throws Exception {
Set<AssemblyProperties> assemblyProperties = new HashSet<AssemblyProperties>();
AssemblyProperties ass=new AssemblyProperties();
ass.setProperty_name("xx");
ass.setProperty_type("List");
assemblyProperties.add(ass);
Assembly assembly =new Assembly();
assembly.setAssembly_properties(assemblyProperties);
assembly.setNumber("aaa");
assembly.setUserId(1);
Test test=new Test();
test.addClass(assembly);
}
}
請幫我解決這個錯誤/提前致謝。當公共setter和私人領域的批註中一類混合
禁止/無法正常工作。 –