0

I have 2 tables: Menu and Course. I mapped there are to 2

package ir.parhoontoos.hibernate;

    import java.io.Serializable;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;

    @Entity
    @Table(name="course")
    public class Course implements Serializable{
        public Course() {
        }

        public Course(String title , String description , byte[] image , Menu menu){
            this.title = title;
            this.description = description;
            this.image = image;
            this.menu = menu;
        }


        private Integer course_id;

        private String title;

        private String description;

        private byte[] image;

        private Menu menu;

        @Id @GeneratedValue
        @Column(name="course_id" , unique = true, nullable = false)
        public Integer getCourse_id() {
            return course_id;
        }
        public void setCourse_id(Integer course_id) {
            this.course_id = course_id;
        }

        @Column(name="title")
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }

        @Column(name="description")
        public String getDescription() {
            return description;
        }
        public void setDescription(String description) {
            this.description = description;
        }

        @Column(name="image")
        public byte[] getImage() {
            return image;
        }
        public void setImage(byte[] image) {
            this.image = image;
        }

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "menu_id", nullable = false , insertable=false , updatable=false)
        public Menu getMenu() {
            return menu;
        }

        public void setMenu(Menu menu) {
            this.menu = menu;
        }
    }

And

package ir.parhoontoos.hibernate;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="menu")
public class Menu implements Serializable{

    public Menu() {
    }

    public Menu(String title) {
        this.title = title;
    }

    @Id @GeneratedValue
    @Column(name="menu_id" , unique = true, nullable = false)
    private int id;

    @Column(name="title")
    private String title;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "menu")
    private Set<Course> posts;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Set<Course> getPosts() {
        return posts;
    }

    public void setPosts(Set<Course> posts) {
        this.posts = posts;
    }

}

Tables:

CREATE TABLE `course` (
  `course_id` int(11) NOT NULL AUTO_INCREMENT,
  `title` text NOT NULL,
  `description` text NOT NULL,
  `image` binary(1) NOT NULL,
  `menu_id` int(11) NOT NULL,
  PRIMARY KEY (`id`,`menu_id`),
  KEY `fk_course_menu_idx` (`menu_id`),
  CONSTRAINT `fk_course_menu` FOREIGN KEY (`menu_id`) REFERENCES `menu` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)

And

CREATE TABLE `menu` (
  `menu_id` int(11) NOT NULL AUTO_INCREMENT,
  `title` text,
  PRIMARY KEY (`id`)
)

when try to create session object i have this exception:

org.hibernate.MappingException: Repeated column in mapping for entity: ir.parhoontoos.hibernate.Course column: id (should be mapped with insert="false" update="false")

maoprogrammer
  • 91
  • 2
  • 4
  • Issue occurs when you map the same database column twice or with the way you join your tables. Need more info about your table structure, and how you access the code. Take a look at these posts [link1](http://stackoverflow.com/questions/15076463/another-repeated-column-in-mapping-for-entity-error), [link2](http://stackoverflow.com/questions/21321814/caused-by-org-hibernate-mappingexception-repeated-column-in-mapping-for-entity) and see if it fits your case. Some theory [here](http://www.tomred.net/tutorials/tomred-java-hibernate-repeated-column-in-mapping-for-entity.html) – Ramzy May 27 '15 at 17:46
  • It's strange , when i run project in local works without error. – maoprogrammer May 27 '15 at 18:06
  • Congrats. Keep the links for reference in future :). Happy Coding – Ramzy May 27 '15 at 18:11
  • I purpose my code run in local without error and in server i have above error – maoprogrammer May 27 '15 at 19:55

0 Answers0