-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Getting Violation of UNIQUE KEY constraint with SQLServer
PostPosted: Tue May 07, 2013 3:00 am 
Newbie

Joined: Tue May 07, 2013 2:38 am
Posts: 2
HI all,
I am getting Violation of UNIQUE KEY constraint 'UQ__tablename__73DEA0412739D489'. Cannot insert duplicate key in object 'dbo.User' only specific to MSSQLServer. but same code which is working with MYSQL database.
I am using "net.sourceforge.jtds.jdbc.Driver"-jtds driver and "org.hibernate.dialect.SQLServer2008Dialect" to connect from hibernate to MSSQLServer 2008 R2.

I have some entities called User, RoleAssociation, Organization

Initially, I am creating user,organization after that i am going to create RoleAssociation for this user and organization. at that time i am getting the above mentioned exception.

I am unable to get what is the issue.
Here is my entities:
User.java
public class User extends DomainObjectWithHexId {

private static final long serialVersionUID = 5716065263130912010L;

private static final Logger LOGGER = Logger.getLogger(User.class);

@ManyToOne
@JoinColumn(name = "ORGANIZATION_ID")
private Organization organization;

@Column(name = "FIRST_NAME", nullable = false)
private String firstName;

@Column(name = "LAST_NAME", nullable = true)
private String lastName;

@Column(name = "EMAIL", nullable = false)
private String email;
setters and getters....
}

Organization.java
public class Organization extends DomainObjectWithHexId {
private static final long serialVersionUID = -4416454826703437112L;

@OneToOne(cascade = CascadeType.ALL)
private AddressDetails addressDetails;

@NotAudited
@Column(name = "SETUP_COMPLETED")
@Type(type = "org.hibernate.type.NumericBooleanType")
private boolean setupCompleted = false;

/**
* Parent for the parent organization is always the same.
*/
@ManyToOne
@JoinColumn(name = "PARENT_ORGANIZATION")
private Organization parent;
setter and getters.
}

RoleAssociation.java

/**
* Class to maintain associations between user, roles, organization and businessOrg.
*
*
*/

@Audited
@Entity
@Table(name = "ROLE_ASSOCIATION_TABLE")
@Inheritance(strategy = InheritanceType.JOINED)
public class RoleAssociation extends DomainObject {
private static final long serialVersionUID = 5142647418174775674L;

@ManyToOne
@JoinColumn(name = "USER_ID", nullable = false)
private User user;

@ManyToOne
@JoinColumn(name = "ROLE_ID", nullable = false)
private Role role;

/**
* the Organization on which business operations are applied and roles are associated.
*/
@ManyToOne
@JoinColumn(name = "BUSINESS_ORG_ID")
private BusinessOrganization businessOrganization;

/**
* the organization to which user is associated with given role.
* Not necessary the org which user belongs to
*/
@ManyToOne
@JoinColumn(name = "ORGANIZATION_ID")
private Organization organization;

/**
* Revoked permissions from a role for a given user.
*/
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "DEACTIVATED_PERMISSIONS", joinColumns = @JoinColumn(name = "ROLE_ASSOCOATION_ID"))
private Set<Permission> deactivatedPermissions = new HashSet<Permission>();

@Column(name = "COMMENTS")
@Type(type = "org.hibernate.type.TextType")
private String comments;

public RoleAssociation() {

}

public RoleAssociation(User user, Role role, BusinessOrganization bussinessOrg) {
super();
Preconditions.checkNotNull(user, "User can not be null.");
this.user = user;
this.role = role;
this.businessOrganization = bussinessOrg;
this.setOrganization(user.getOrganization());
}

/**
* Use for partner association
*/
public RoleAssociation(User user, Role role, BusinessOrganization businessOrganization, Organization partnerOrganization) {
super();
this.user = user;
this.role = role;
this.businessOrganization = businessOrganization;
this.setOrganization(partnerOrganization);
}

public RoleAssociation(User user, Role role, BusinessOrganization businessOrganization, Organization partnerOrganization,
Set<Permission> deactivatedPermissions) {
super();
this.user = user;
this.role = role;
this.businessOrganization = businessOrganization;
this.setOrganization(partnerOrganization);
this.deactivatedPermissions = deactivatedPermissions;
}

public RoleAssociation(User user, Role role, BusinessOrganization bussinessOrg, Set<Permission> deactivatedPermissions) {
super();
this.user = user;
this.role = role;
this.businessOrganization = bussinessOrg;
this.setOrganization(user.getOrganization());
this.deactivatedPermissions = deactivatedPermissions;
}

public void setUser(User user) {
this.user = user;
}

public User getUser() {
return user;
}

public void setRole(Role role) {
this.role = role;
}

public Role getRole() {
return role;
}

public void setBusinessOrganization(BusinessOrganization taxableOrg) {
this.businessOrganization = taxableOrg;
}

public BusinessOrganization getBusinessOrganization() {
return businessOrganization;
}

public void setOrganization(Organization organization) {
this.organization = organization;
}

public Organization getOrganization() {
return organization;
}

public void setDeactivatedPermissions(Set<Permission> deactivatedPermissions) {
this.deactivatedPermissions = deactivatedPermissions;
}

public Set<Permission> getDeactivatedPermissions() {
return deactivatedPermissions;
}

public String getComments() {
return comments;
}

public void setComments(String comments) {
this.comments = comments;
}

}

Please help me on this issue.


Top
 Profile  
 
 Post subject: Re: Getting Violation of UNIQUE KEY constraint with SQLServer
PostPosted: Tue May 07, 2013 7:26 am 
Newbie

Joined: Tue May 07, 2013 2:38 am
Posts: 2
Got the problem... there is an issue with SqlServer, which wont allow multiple null for unique constraint column. but others like MySql we can insert multiple null values for unique constraint column.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.