ManyToMany中重复输入键'PRIMARY'

时间:2015-09-28 15:55:05

标签: hibernate

我最近收到了一些创建ManyToMany关系的帮助。代码似乎有效。我的表看起来像:

house_id is PK and NN
username is PK and NN

并可能显示为

house_id   username
  41            t
  41            tea
  40            tea

但是,每次我向这个表添加一些内容时它都是第一次运行,但是当我尝试添加其他内容时,我会在''主键上出现重复错误。 当我添加

house_Id 22 and username 2

它第一次运作。但如果在那之后我添加

house_id 24 and username 2

hibernate会给我一个例外:

ERROR: Duplicate entry 'tea-22' for key 'PRIMARY'

我不明白为什么Hibernate抱怨'tea-22',当我已经把那个成功地放在那里,现在试图保存'茶-24'而不是。就像有一些缓存没有冲出'tea-22'。

@Entity
public class User {

@Id
private String username;
private String firstname;
private String lastname;



@ManyToMany
@JoinTable(name = "house_user_association", joinColumns = {
                @JoinColumn(name = "username", referencedColumnName = "username")},
                inverseJoinColumns = {@JoinColumn(name = "house_id", referencedColumnName = "projectId")})  
        private Set<House> houses = new HashSet<>();

public User() {
}

public User(String username) {
    this.username = username;
}

public Collection<Houses> getHouses() {
    return houses;
}

public void setHouses(Collection<Marker> houses) {
    this.houses = houses;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}
}

众议院。

@Entity
@Table(name="house")
public class House{


    private String address; 

    @ManyToMany(mappedBy = "bookmarks")
    private Set<User> users = new HashSet<>();

    @Id  @GeneratedValue(strategy=GenerationType.AUTO)
    private long projectId; 

    public House() {

    }

    public House(String address) {
        this.address = address;
    }

    public long getProjectId() {
        return projectId;
    }

    public void setProjectId(long projectId) {
        this.projectId = projectId;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

Servlet方法:

 Session session1 = null;
        String output = "";
        boolean contains = true;
        Long houseId = null;
        House house = null;

try {

            String add = request.getParameter("houseId");
            System.out.println("Trying to insert: " + add);
            session1 = hiber.getSessionFactory().openSession();
            session1.beginTransaction();
            house = (House) session1.get(Marker.class, Long.parseLong(add));
            houseId = house.getProjectId();
            System.out.println("fetched houseID: " + houseId);
            session1.getTransaction().commit();
            session1.close();

            for (Iterator<House> iterator = SessionUser.getHouses().iterator(); iterator.hasNext();) {
                House houseIterate = iterator.next();
                if (houseIterate.getProjectId() == houseId) {
                    contains = false;
                    System.out.println(houseIterate.getProjectId());
                }
            }   
            System.out.println(contains);
            if(contains){
                session1 = hiber.getSessionFactory().openSession();
                session1.beginTransaction();
                System.out.println("fetched houseID: " + house.getProjectId());
                SessionUser.getHouses().add(house);
                session1.merge(SessionUser);
                session1.flush();
                session1.getTransaction().commit();

                output = "[" +"/" +marker.getLat() + "/" + marker.getLng() + "/" + marker.getProjectId() + "/" + marker.getAddress()+ "/" +"]";
                out.print(output);  
                System.out.println(output);     
                System.out.println("Closing hibernate Session");    
            }
            if(!contains){
                System.out.println("Already contains"); 
            }

        } catch (Exception e) {
            System.out.println(e);
        } finally {
            house = null;
            houseId = null;
            contains = true;
            System.out.println("Test"); 
            if(session1 != null){
                session1.close();
            }       
        }

0 个答案:

没有答案