JPA - 错误的重复外键

时间:2014-08-24 02:02:12

标签: java hibernate jpa

我正在使用@ElementCollectionon一个用于角色类型的集合,但它插入了第一个正在测试的主键,同时它添加了第三个条目但是是一个重复键。这是错误的关键。我不确定它是否在我的测试中或者实际上我是如何执行实体管理器的。正如您所看到的,user_id只有第一个ID,而不是用户Role表上的第二个ID。 enter image description here enter image description here DAO

@Entity
@Table(name = "users")
public class DefaultUser implements Serializable
{
    @Id
    @SequenceGenerator(name="user_id_seq_gen", sequenceName="USER_ID_SEQ")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="user_id_seq_gen") 
    @Column(name = "user_id", nullable = false)
    @OrderColumn
    private long      user_id;

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

    @Column(name = "first_name")
    private String  firstName;

    @Column(name = "last_name")
    private String  lastName;

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



    @ElementCollection(targetClass=UserType.class, fetch = FetchType.EAGER)
    @CollectionTable(
            name="user_roles",
            joinColumns=@JoinColumn(name="user_id")
      )
    @Enumerated(EnumType.STRING)
    @Column(name="role_type")
    @OrderColumn
    private Set<UserType> userRoleSet = new HashSet<UserType>();

//getters and setters
}

测试方法

    public class UserDTOTest
    {

    JpaUserDAO userDAO = new JpaUserDAO(EntityManagerUtil.getEntityManager());

    DefaultUser user = new DefaultUser();
    user.setUser_name("user");
    user.setPassword("ddafsf");
    user.setFirstName("dadafddfsffsd");
    user.setLastName("dafddfafdfsasdf");
    Set<UserType> userTypeSet = new HashSet<UserType>();
    userTypeSet.add(UserType.BROKER);
    userTypeSet.add(UserType.SHAREHOLDER);

    user.setUserRoleSet(userTypeSet);
    userDAO.persist(user);

   //JpaUserDAO userDAO1 = new JpaUserDAO(EntityManagerUtil.getEntityManager());
    DefaultUser user2 = new DefaultUser();
    user2.setUser_name("user1");
    user2.setPassword("a342fsd");
    user2.setFirstName("some54thing");
    user2.setLastName("diff435ernt");
    Set<UserType> userTypeSet2 = new HashSet<UserType>();
    userTypeSet.add(UserType.ADMIN);

    user2.setUserRoleSet(userTypeSet2);
    userDAO.persist(user2);

        }
    }

JPA DAO

 public abstract class JpaDao<K, E> implements DAO<K, E>
    {
    protected Class<E>      entityClass;

protected EntityManager entityManager;

public JpaDao(EntityManager entityManager)
{
this.entityManager = entityManager;
ParameterizedType genericSuperclass = (ParameterizedType) getClass()
    .getGenericSuperclass();
this.entityClass = (Class<E>) genericSuperclass
    .getActualTypeArguments()[1];
}

/**
 * Create a new user record in the database.
 *
 * @param entity
 *            User to be created.
 * @return the ID of saved User.
 */
public void persist(E entity)
{
    entityManager.getTransaction().begin();
    entityManager.persist(entity);
    entityManager.getTransaction().commit();
}

public void remove(E entity)
{
    entityManager.getTransaction().begin();
    entityManager.remove(entity);
    entityManager.getTransaction().commit();
}

public E findById(K id)
{
    entityManager.getTransaction().begin();
    entityManager.getTransaction().commit();
    return entityManager.find(entityClass, id);

}

}

1 个答案:

答案 0 :(得分:1)

您应该将userTypeSet.add(UserType.ADMIN); 替换为userTypeSet2.add(UserType.ADMIN);