2010-09-22 8 views
50

अब, मैं हाइबरनेट सीख रहा हूं, और शुरू किया मेरे प्रोजेक्ट में इसका इस्तेमाल करने के लिए। यह एक सीआरयूडी आवेदन है। मैंने सभी क्रूड ऑपरेशंस के लिए हाइबरनेट का उपयोग किया। यह उन सभी के लिए काम करता है। लेकिन, एक से कई & कई से एक, मैं इसे आजमाने के थक गया हूँ। अंततः यह मुझे नीचे त्रुटि देता है।org.hibernate.MappingException: के लिए प्रकार निर्धारित नहीं कर सका: java.util.List, टेबल पर: कॉलेज, कॉलम के लिए: [org.hibernate.mapping.Column (छात्र)]

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]

फिर मैं इस video tutorial माध्यम से चला गया। शुरुआत में, मेरे लिए यह बहुत आसान है। लेकिन, मैं इसे काम नहीं कर सकता। यह भी अब कहते हैं,

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)]

मैं कुछ खोजों इंटरनेट में भाग गया है, वहाँ किसी को बताने its a bug in Hibernate, और कुछ का कहना है कि @GenereatedValue इस त्रुटि को मंजूरी दे दी हो जाएगा जोड़कर। लेकिन, मेरे लिए नोटिंग काम करता है,

मुझे आशा है कि मुझे कुछ ठीक मिलेगा !!

धन्यवाद!

यहाँ मेरी कोड:

College.java

@Entity 
public class College { 
@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
private int collegeId; 
private String collegeName; 


private List<Student> students; 

@OneToMany(targetEntity=Student.class, mappedBy="college", fetch=FetchType.EAGER) 
public List<Student> getStudents() { 
    return students; 
} 
public void setStudents(List<Student> students) { 
    this.students = students; 
}//Other gettters & setters omitted 

Student.java

@Entity 
public class Student { 


@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
private int studentId; 
private String studentName; 


private College college; 

@ManyToOne 
@JoinColumn(name="collegeId") 
public College getCollege() { 
    return college; 
} 
public void setCollege(College college) { 
    this.college = college; 
}//Other gettters & setters omitted 

Main.java:

public class Main { 

private static org.hibernate.SessionFactory sessionFactory; 

    public static SessionFactory getSessionFactory() { 
    if (sessionFactory == null) { 
     initSessionFactory(); 
    } 
    return sessionFactory; 
    } 

    private static synchronized void initSessionFactory() { 
    sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 

    } 

    public static Session getSession() { 
    return getSessionFactory().openSession(); 
    } 

    public static void main (String[] args) { 
       Session session = getSession(); 
     Transaction transaction = session.beginTransaction(); 
     College college = new College(); 
     college.setCollegeName("Dr.MCET"); 

     Student student1 = new Student(); 
     student1.setStudentName("Peter"); 

     Student student2 = new Student(); 
     student2.setStudentName("John"); 

     student1.setCollege(college); 
     student2.setCollege(college); 



     session.save(student1); 
     session.save(student2); 
     transaction.commit(); 
    } 


} 

कंसोल:

Exception in thread "main" org.hibernate.MappingException: Could not determine type for: java.util.List, at table: College, for columns: [org.hibernate.mapping.Column(students)] 
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306) 
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:290) 
at org.hibernate.mapping.Property.isValid(Property.java:217) 
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:463) 
at org.hibernate.mapping.RootClass.validate(RootClass.java:235) 
at org.hibernate.cfg.Configuration.validate(Configuration.java:1330) 
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1833) 
at test.hibernate.Main.initSessionFactory(Main.java:22) 
at test.hibernate.Main.getSessionFactory(Main.java:16) 
at test.hibernate.Main.getSession(Main.java:27) 
at test.hibernate.Main.main(Main.java:43) 

एक्सएमएल:

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
<session-factory> 
    <!-- Database connection settings --> 
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="connection.url">jdbc:mysql://localhost:3306/dummy</property> 
    <property name="connection.username">root</property> 
    <property name="connection.password">1234</property> 
    <!-- JDBC connection pool (use the built-in) --> 
    <property name="connection.pool_size">1</property> 
    <!-- SQL dialect --> 
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
    <!-- Enable Hibernate's automatic session context management --> 
    <property name="current_session_context_class">thread</property> 
    <!-- Disable the second-level cache --> 
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 
    <!-- Echo all executed SQL to stdout --> 
    <property name="show_sql">true</property> 
    <!-- Drop and re-create the database schema on startup --> 
    <property name="hbm2ddl.auto">update</property> 

    <mapping class="test.hibernate.Student" /> 
    <mapping class="test.hibernate.College" /> 
</session-factory> 

+0

@All अधिक विचार के लिए के लिए आप

कि त्रुटि की तरह दिखाई देगा: कोई भी मुझे के बारे में, Main.java में :( –

+0

बोलना कोड गलत जगह रखना समस्या सूची वस्तु –

उत्तर

104

आप क्षेत्र पहुँच रणनीति (@Id एनोटेशन द्वारा निर्धारित) का उपयोग कर रहे हैं।

@Column 
    @ElementCollection(targetClass=Integer.class) 
    private List<Integer> countries; 
+0

के निर्माण हो सकता है बताया @ आर्थर रोनाल्ड एफडी गार्सिया: धन्यवाद, यह बहुत अच्छा काम करता है। लेकिन, कार्यक्रम अब नए द्वारा बंद कर दिया गया है। 'ऑब्जेक्ट एक असुरक्षित क्षणिक उदाहरण का संदर्भ देता है - फ्लश करने से पहले क्षणिक उदाहरण को बचाएं' क्या आप इस त्रुटि से अवगत हैं। अगर इसे छोड़ दें। मैं खोज रहा हूं। –

+0

@ आर्थर रोनाल्ड एफडी गार्सिया: हाय फिर से, मैंने अपनी वर्तमान त्रुटि के लिए समाधान देखा, जिसे मैंने अपनी पिछली टिप्पणी में पूछा था। यह लिंक है। Http://stackoverflow.com/questions/1667177/nhibernate-error -सेव-द-क्षणिक-उदाहरण-पहले-फ्लशिंग समाधान 'cascade = CascadeType.ALL' सहित है। लेकिन मेरे लिए यह त्रुटि और मेरे ग्रहण में कोई सुझाव नहीं दिखाता है। अपको इस बारे में कुछ पता है। :) –

+3

@ आर्थर +1 अच्छा पकड़ @MaRaVan आप या तो फील्ड एक्सेस रणनीति या संपत्ति पहुंच रणनीति का उपयोग कर सकते हैं लेकिन आपको कक्षा पदानुक्रम के भीतर लगातार होना चाहिए, आप कम से कम जेपीए 1.0 में रणनीतियों को मिश्रित नहीं कर सकते हैं। जेपीए 2.0 हालांकि रणनीतियों को मिश्रण करना संभव बनाता है, लेकिन आपको अतिरिक्त एनोटेशन (और इस प्रकार अधिक जटिलता) की आवश्यकता होगी। तो सिफारिश एक रणनीति चुनना और अपने आवेदन में चिपकना है। देखें [2.2.2.2। एक्सेस प्रकार] (http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e535)। –

36

@ElementCollection सूची क्षेत्र के लिए इस मुद्दे को हल जोड़ने के बजाय प्रत्येक क्षेत्र के ऊपर किसी भी जेपीए संबंधित एनोटेशन रखो @Access(AccessType.PROPERTY)

+2

यह मूल प्रकार की सूचियों का समाधान है। – phabtar

+0

+ 'targetClass 'आवश्यक नहीं है क्योंकि आपने सूची' 'के प्रकार का उल्लेख किया है। –

4
@Access(AccessType.PROPERTY) 
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER) 
@JoinColumn(name="userId") 
public User getUser() { 
    return user; 
} 

मैं एक ही समस्या है, मैं इसे हल: सही गेटर संपत्ति

@OneToMany(targetEntity=Student.class, mappedBy="college", fetch=FetchType.EAGER) 
private List<Student> students; 
0

चिंता न करें! यह समस्या एनोटेशन के कारण होती है। मुझे इसी तरह का सामना करना पड़ा।

यह सही कोड है:

package onetomanymapping; 

import java.util.List; 

import javax.persistence.*; 

@Entity 
public class College { 
private int collegeId; 
private String collegeName; 

private List<Student> students; 

@OneToMany(targetEntity = Student.class, mappedBy = "college", cascade = CascadeType.ALL, fetch = FetchType.EAGER) 

public List<Student> getStudents() { 
    return students; 
} 

public void setStudents(List<Student> students) { 
    this.students = students; 
} 

@Id 
@GeneratedValue 
public int getCollegeId() { 
    return collegeId; 
} 

public void setCollegeId(int collegeId) { 
    this.collegeId = collegeId; 
} 

public String getCollegeName() { 
    return collegeName; 
} 

public void setCollegeName(String collegeName) { 
    this.collegeName = collegeName; 
} 

}

9

पहुँच रणनीतियों

एक जेपीए प्रदाता के रूप में के साथ समस्या, हाइबरनेट दोनों इकाई (उदाहरण के क्षेत्रों) विशेषताओं का आत्मनिरीक्षण कर सकते हैं या एक्सेसर्स (उदाहरण गुण)। डिफ़ॉल्ट रूप से, @Id एनोटेशन की नियुक्ति डिफ़ॉल्ट पहुंच रणनीति प्रदान करती है। जब किसी फ़ील्ड पर रखा जाता है, तो हाइबरनेट फ़ील्ड-आधारित पहुंच मान लेगा। पहचानकर्ता गेटर पर रखा गया, हाइबरनेट संपत्ति-आधारित पहुंच का उपयोग करेगा।

फील्ड आधारित पहुंच

क्षेत्र के आधार पर पहुँच का उपयोग कर, अन्य संस्था स्तर की विधियों को जोड़ने और अधिक लचीला है क्योंकि हाइबरनेट हठ राज्य

@Entity 
public class Simple { 

@Id 
private Integer id; 

@OneToMany(targetEntity=Student.class, mappedBy="college", 
fetch=FetchType.EAGER) 
private List<Student> students; 

//getter +setter 
} 
के उन लोगों के भाग पर विचार नहीं करेंगे

संपत्ति-आधारित पहुंच

संपत्ति-आधारित पहुंच का उपयोग करते समय, हाइबरनेट ई दोनों पढ़ने और इकाई राज्य

@Entity 
public class Simple { 

private Integer id; 
private List<Student> students; 

@Id 
public Integer getId() { 
    return id; 
} 

public void setId(Integer id) { 
    this.id = id; 
} 
@OneToMany(targetEntity=Student.class, mappedBy="college", 
fetch=FetchType.EAGER) 
public List<Student> getStudents() { 
    return students; 
} 
public void setStudents(List<Student> students) { 
    this.students = students; 
} 

} 

लिखने के लिए accessors का उपयोग करता है लेकिन आप एक ही समय में दोनों फील्ड के आधार पर और संपत्ति-आधारित पहुँच का उपयोग नहीं कर सकते हैं। यह पालन this

संबंधित मुद्दे