org.hibernate.MappingException:无法确定类型:java.util.Collection,对于列:[org.hibernate.mapping.Column(lisOfAddresses)]

时间:2017-05-21 18:52:32

标签: java mysql hibernate

我正在制作一个hibernate小应用程序,其中我得到以下异常

  

线程“main”中的异常org.hibernate.MappingException:无法确定类型:java.util.Collection,对于列:[org.hibernate.mapping.Column(lisOfAddresses)]

我已阅读了几篇帖子但无法找到解决方案。 任何人都可以告诉我为什么异常即将来临。代码如下:

UserDetails.java

  package com.sdnext.hibernate.tutorial.dto;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Table;

import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

@Entity
@Table (name="USER_DETAIL")
public class UserDetails 
{
    @Id
    @Column(name="USER_ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int    userId;

    @Column(name="USER_NAME") 
    private String userName;

    @ElementCollection
    @JoinTable(name="USER_ADDRESS",
            joinColumns=@JoinColumn(name="USER_ID"))
    @GenericGenerator(strategy="hilo", name = "hilo-gen")
    @CollectionId(columns = { @Column(name="ADDRESS_ID") }, generator = "hilo-gen", type = @Type(type="long"))
    private Collection<Address> lisOfAddresses = new ArrayList<Address>();

    public Collection<Address> getLisOfAddresses() {
        return lisOfAddresses;
    }
    public void setLisOfAddresses(Collection<Address> lisOfAddresses) {
        this.lisOfAddresses = lisOfAddresses;
    }
    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;
    }
    public String toString()
    {
        return "[User Name: "+userName+"\n Office Address: "+lisOfAddresses+"]";
    }
}

HibernateTestDemo.java

    package com.sdnext.hibernate.tutorial;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import com.sdnext.hibernate.tutorial.dto.Address;
import com.sdnext.hibernate.tutorial.dto.UserDetails;

public class HibernateTestDemo {
      public static void main(String[] args) 
        {
          UserDetails user = new UserDetails();
            //user.setUserId(1);
            user.setUserName("Dinesh Rajput");

            Address address1 = new Address();
            address1.setStreet("First Street");
            address1.setCity("First City");
            address1.setState("First State");
            address1.setPincode("First Pin");

            Address address2 = new Address();
            address2.setStreet("Second Street");
            address2.setCity("Second City");
            address2.setState("Second State");
            address2.setPincode("Second Pin");

            user.getLisOfAddresses().add(address1);
            user.getLisOfAddresses().add(address2);

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

Address.java

 package com.sdnext.hibernate.tutorial.dto;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable //for value object it is not is entity object. Value object means does not have real meaning for self individually.
public class Address
{
    @Column(name="STREET_NAME")
    private String street;
    @Column(name="CITY_NAME")
    private String city;
    @Column(name="STATE_NAME")
    private String state;
    @Column(name="PIN_CODE")
    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;
    }
    public String toString()
    {
        return " {Street: "+street+" City: "+city+" State: "+state+" Pincode: "+pincode+" }";
    }
}

hibernate.cfg.xml中

 <?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/hibernateDB</property> 
         <property name="connection.username">root</property> 
         <property name="connection.password"></property> 
         <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> 

        <!-- Show 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="com.sdnext.hibernate.tutorial.dto.UserDetails"/>

     </session-factory> 
 </hibernate-configuration>

2 个答案:

答案 0 :(得分:0)

根据您的实体结构 UserDetails,可以有多个Address

Address实体没有指向UserDetails

的链接

请更改

@ElementCollection
@JoinTable(name="USER_ADDRESS", joinColumns=@JoinColumn(name="USER_ID"))
@GenericGenerator(strategy="hilo", name = "hilo-gen")
@CollectionId(columns = { @Column(name="ADDRESS_ID") }, generator = "hilo-gen", type = @Type(type="long"))
private Collection<Address> lisOfAddresses = new ArrayList<Address>();

@OneToMany(mappedBy="userDetails")
Collection<Address> colAddress;

并在Address实体添加

@ManyToOne
@JoinColumn(name="USER_DETAIL_ID")
public UserDetails userDetails;

答案 1 :(得分:0)

在评论中说它太长了,所以iam在这里张贴,以下是我的pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.technicalkeeda</groupId>
    <artifactId>HibernateExamples</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>Spring and Hibernate Examples</name>
    <properties>
        <spring.version>4.3.7.RELEASE</spring.version>
        <spring.security.version>4.2.1.RELEASE</spring.security.version>
        <hibernate.version>4.3.9.Final</hibernate.version>
        <servlet.api.version>3.1.0</servlet.api.version>
        <jsp.api.version>2.2</jsp.api.version>
        <jstl.version>1.2</jstl.version>
        <jdk.version>1.8</jdk.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- <dependency> -->
        <!-- <groupId>mysql</groupId> -->
        <!-- <artifactId>mysql-connector-java</artifactId> -->
        <!-- <version>5.1.9</version> -->
        <!-- </dependency> -->

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>

        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm</artifactId>
            <version>3.1</version>
        </dependency>

        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.3</version>
        </dependency>
        <!-- <dependency> -->
        <!-- <groupId>org.slf4j</groupId> -->
        <!-- <artifactId>slf4j-api</artifactId> -->
        <!-- <version>1.7.25</version> -->
        <!-- </dependency> -->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- JavaConfig need this library -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>

        <!-- Spring AOP + AspectJ -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

    </dependencies>
</project>