- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 junit 下运行测试时遇到了一些麻烦。我收到此错误:
29/04/2014 22:39:43 INFO Version - HHH000412: Hibernate Core {4.3.5.Final}
...
29/04/2014 22:39:44 INFO SchemaValidator - HHH000229: Running schema validator
29/04/2014 22:39:44 INFO SchemaValidator - HHH000102: Fetching database metadata
29/04/2014 22:39:44 INFO TableMetadata - HHH000261: Table found: PUBLIC.SECURITY.GROUP_USER
29/04/2014 22:39:44 INFO TableMetadata - HHH000037: Columns: [usr_id, grp_id]
29/04/2014 22:39:44 INFO TableMetadata - HHH000261: Table found: PUBLIC.SECURITY.GROUP
29/04/2014 22:39:44 INFO TableMetadata - HHH000037: Columns: [grp_name, grp_id]
29/04/2014 22:39:44 INFO TableMetadata - HHH000261: Table found: PUBLIC.SECURITY.USER
29/04/2014 22:39:44 INFO TableMetadata - HHH000037: Columns: [usr_firstname, usr_id, usr_lastname, usr_expiration, usr_email, usr_modification, usr_login, usr_password, usr_creation, usr_enabled]
29/04/2014 22:39:45 INFO ResourceDatabasePopulator - Executing SQL script from class path resource [database/populate-security-data.sql]
29/04/2014 22:39:45 INFO ResourceDatabasePopulator - Done executing SQL script from class path resource [database/populate-security-data.sql] in 11 ms.
29/04/2014 22:39:45 INFO TransactionalTestExecutionListener - Began transaction (1) for test context [DefaultTestContext@7c748639 testClass = SecurityTest, testInstance = SMP.pub.example.SecurityTest@46184804, testMethod = permissionTest@SecurityTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@6420228f testClass = SecurityTest, locations = '{classpath:spring/test-context.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@79bd0da3]; rollback [true]
29/04/2014 22:39:45 DEBUG SqlStatementLogger - select SPuser0_.usr_id as usr_id1_7_0_, SPuser0_.usr_creation as usr_crea2_7_0_, SPuser0_.usr_email as usr_emai3_7_0_, SPuser0_.usr_enabled as usr_enab4_7_0_, SPuser0_.usr_expiration as usr_expi5_7_0_, SPuser0_.usr_firstname as usr_firs6_7_0_, SPuser0_.usr_lastname as usr_last7_7_0_, SPuser0_.usr_modification as usr_modi8_7_0_, SPuser0_.usr_password as usr_pass9_7_0_, SPuser0_.usr_login as usr_log10_7_0_ from security.user SPuser0_ where SPuser0_.usr_id=?
29/04/2014 22:39:45 TRACE BasicBinder - binding parameter [1] as [BIGINT] - [1]
29/04/2014 22:39:45 TRACE BasicExtractor - extracted value ([usr_crea2_7_0_] : [TIMESTAMP]) - [2014-04-29 22:39:45.323]
29/04/2014 22:39:45 TRACE BasicExtractor - extracted value ([usr_emai3_7_0_] : [VARCHAR]) - [commercial.63-1@sample.com]
29/04/2014 22:39:45 TRACE BasicExtractor - extracted value ([usr_enab4_7_0_] : [BOOLEAN]) - [true]
29/04/2014 22:39:45 TRACE BasicExtractor - extracted value ([usr_expi5_7_0_] : [TIMESTAMP]) - [2015-04-03 09:48:52.0]
29/04/2014 22:39:45 TRACE BasicExtractor - extracted value ([usr_firs6_7_0_] : [VARCHAR]) - [commercial]
29/04/2014 22:39:45 TRACE BasicExtractor - extracted value ([usr_last7_7_0_] : [VARCHAR]) - [63-1]
29/04/2014 22:39:45 TRACE BasicExtractor - extracted value ([usr_modi8_7_0_] : [TIMESTAMP]) - [2014-04-29 22:39:45.323]
29/04/2014 22:39:45 TRACE BasicExtractor - extracted value ([usr_pass9_7_0_] : [VARCHAR]) - []
29/04/2014 22:39:45 TRACE BasicExtractor - extracted value ([usr_log10_7_0_] : [VARCHAR]) - [commercial.63-1]
29/04/2014 22:39:45 TRACE CollectionType - Created collection wrapper: [com.sample.security.metier.impl.SPUser.groups#1]
...
29/04/2014 22:39:45 DEBUG SqlStatementLogger - select SPgroup0_.grp_id as grp_id1_2_0_, SPgroup0_.grp_name as grp_name2_2_0_ from security.group SPgroup0_ where SPgroup0_.grp_id=?
29/04/2014 22:39:45 TRACE BasicBinder - binding parameter [1] as [BIGINT] - [4]
29/04/2014 22:39:45 TRACE BasicExtractor - extracted value ([grp_name2_2_0_] : [VARCHAR]) - [Commercial 63]
29/04/2014 22:39:45 TRACE CollectionType - Created collection wrapper: [com.sample.security.metier.impl.SPGroup.childGroups#4]
29/04/2014 22:39:45 TRACE CollectionType - Created collection wrapper: [com.sample.security.metier.impl.SPGroup.parentGroups#4]
29/04/2014 22:39:45 TRACE CollectionType - Created collection wrapper: [com.sample.security.metier.impl.SPGroup.permissionTags#4]
29/04/2014 22:39:45 TRACE CollectionType - Created collection wrapper: [com.sample.security.metier.impl.SPGroup.roleTags#4]
29/04/2014 22:39:45 TRACE CollectionType - Created collection wrapper: [com.sample.security.metier.impl.SPGroup.users#4]
...
29/04/2014 07:46:50 DEBUG SqlStatementLogger - select groups0_.usr_id as usr_id2_7_0_, groups0_.grp_id as grp_id1_1_0_, SPgroup1_.grp_id as grp_id1_2_1_, SPgroup1_.grp_name as grp_name2_2_1_ from group_user groups0_ inner join security.group SPgroup1_ on groups0_.grp_id=SPgroup1_.grp_id where groups0_.usr_id=?
30/04/2014 07:46:50 WARN SqlExceptionHelper - SQL Error: -5501, SQLState: 42501
** 30/04/2014 07:46:50 ERROR SqlExceptionHelper - user lacks privilege or object not found: GROUP_USER **
这是发生错误的代码部分:
public Set<Role> getRoles() {
Set<Role> roles = new HashSet<Role>();
List<SPGroup> localGroups = getGroups();
if (localGroups != null) {
** for (SPGroup group : localGroups) { **
roles.addAll(group.getRoles());
}
}
return roles;
}
getGroups 函数运行良好,但对 localGroups 的迭代会抛出此错误。
这是我的代码和配置:
SP用户实体
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name="user", schema="security")
public class SPUser implements User {
private static final long serialVersionUID = 6335411953406117295L;
@Id
@GeneratedValue
@Column(name = "usr_id", unique = true, nullable = false)
private Long id;
@Column(name = "usr_password", nullable = false, length=100)
private String password;
@Column(name = "usr_login", unique=true, nullable = false, length=128)
private String username;
@Column(name = "usr_enabled", nullable = false)
private boolean enabled;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "usr_expiration")
private Date expirationDate;
@Column(name = "usr_firstname", length=50)
private String firstname;
@Column(name = "usr_lastname", length=50)
private String lastname;
@Column(name = "usr_email", unique=true, length=128)
private String email;
@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "usr_creation")
private Date creationDate;
@LastModifiedDate
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "usr_modification")
private Date modificationDate;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "users")
private List<SPGroup> groups;
...
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Date getExpirationDate() {
return expirationDate;
}
public void setExpirationDate(Date expirationDate) {
this.expirationDate = expirationDate;
}
@Override
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
@Override
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
@Override
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Date getModificationDate() {
return modificationDate;
}
public void setModificationDate(Date modificationDate) {
this.modificationDate = modificationDate;
}
@Override
public List<SPGroup> getGroups() {
return groups;
}
public void setGroups(List<SPGroup> groups) {
this.groups = groups;
}
public Set<Role> getRoles() {
Set<Role> roles = new HashSet<Role>();
List<SPGroup> localGroups = getGroups();
if (localGroups != null) {
for (SPGroup group : localGroups) {
roles.addAll(group.getRoles());
}
}
return roles;
}
@Override
public Map<SPPermission, SPPerimeter> getPermissions() {
return SPGroup.mergePermissionsFromGroups(getGroups());
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return getRoles();
}
@Override
public boolean isAccountNonExpired() {
if (getExpirationDate() == null) {
return true;
} else {
return Calendar.getInstance().getTime().before(getExpirationDate());
}
}
@Override
public boolean isAccountNonLocked() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isCredentialsNonExpired() {
// TODO Auto-generated method stub
return true;
}
...
}
SP 组实体
@Entity
@Table(name="group", schema="security")
public class SPGroup implements Group {
@Id
@GeneratedValue
@Column(name = "grp_id", unique = true, nullable = false)
private Long id;
@Column(name = "grp_name", unique = true, nullable = false, length=150)
private String name;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "group_user", joinColumns = {
@JoinColumn(name = "grp_id", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "usr_id",
nullable = false, updatable = false) })
private List<SPUser> users = new ArrayList<SPUser>();
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "childGroups")
private List<SPGroup> parentGroups = new ArrayList<SPGroup>();
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "group_group", joinColumns = {
@JoinColumn(name = "parent_id", nullable = false, updatable = false, referencedColumnName="grp_id") },
inverseJoinColumns = { @JoinColumn(name = "child_id",
nullable = false, updatable = false, referencedColumnName="grp_id") })
private List<SPGroup> childGroups = new ArrayList<SPGroup>();
@OneToMany(fetch = FetchType.LAZY, targetEntity=RoleTag.class ,mappedBy = "group", cascade={CascadeType.ALL})
private List<RoleTag> roleTags = new ArrayList<RoleTag>();
@OneToMany(fetch = FetchType.LAZY, targetEntity=PermissionTag.class, mappedBy = "group", cascade={CascadeType.ALL})
private List<PermissionTag> permissionTags = new ArrayList<PermissionTag>();
...
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<SPUser> getUsers() {
return users;
}
public void setUsers(List<SPUser> users) {
this.users = users;
}
public void addUser(SPUser user) {
if (user.getGroups() == null) {
user.setGroups(new ArrayList<SPGroup>());
}
if (!user.getGroups().contains(this)) {
user.getGroups().add(this);
}
getUsers().add(user);
}
public List<SPGroup> getParentGroups() {
return parentGroups;
}
public void setParentGroups(List<SPGroup> parentGroups) {
this.parentGroups = parentGroups;
}
public List<SPGroup> getChildGroups() {
return childGroups;
}
public void setChildGroups(List<SPGroup> childGroups) {
this.childGroups = childGroups;
}
public List<RoleTag> getRoleTags() {
return roleTags;
}
public void setRoleTags(List<RoleTag> roleTags) {
this.roleTags = roleTags;
}
public List<PermissionTag> getPermissionTags() {
return permissionTags;
}
public void setPermissionTags(List<PermissionTag> permissionTags) {
this.permissionTags = permissionTags;
}
public void addPermissionTag(PermissionTag permissionTag) {
permissionTag.setGroup(this);
getPermissionTags().add(permissionTag);
}
@Override
public List<SPUser> getAllUsers() {
List<SPUser> allUsers = new ArrayList<SPUser>(getUsers());
for (SPGroup childGroup : getAllChildGroups()) {
for (SPUser user : childGroup.getUsers()) {
if (!allUsers.contains(user)) {
allUsers.add(user);
}
}
}
return allUsers;
}
@Override
public List<SPGroup> getAllParentGroups() {
return getAllParentGroups(new HashSet<SPGroup>(Arrays.asList(this)));
}
private List<SPGroup> getAllParentGroups(Set<SPGroup> ignoreSet) {
List<SPGroup> allparentGroups = new ArrayList<SPGroup>();
for (SPGroup parent : getParentGroups()) {
if (!ignoreSet.contains(parent)) {
ignoreSet.add(parent);
allparentGroups.add(parent);
allparentGroups.addAll(parent.getAllParentGroups(ignoreSet));
}
}
return allparentGroups;
}
@Override
public List<SPGroup> getAllChildGroups() {
return getAllChildGroups(new HashSet<SPGroup>(Arrays.asList(this)));
}
private List<SPGroup> getAllChildGroups(Set<SPGroup> ignoreSet) {
List<SPGroup> allChildrenGroups = new ArrayList<SPGroup>();
for (SPGroup child : getChildGroups()) {
if (!ignoreSet.contains(child)) {
ignoreSet.add(child);
allChildrenGroups.add(child);
allChildrenGroups.addAll(child.getAllChildGroups(ignoreSet));
}
}
return allChildrenGroups;
}
@Override
public Set<Role> getRoles() {
return getRoles(true);
}
private Set<Role> getRoles(boolean merged) {
if (merged) {
return getActiveRoles(getMergedRoleMap());
} else {
return getActiveRoles(getRoleMap());
}
}
private Map<Role, Boolean> getRoleMap() {
Map<Role, Boolean> roleMap = new HashMap<Role, Boolean>();
for (RoleTag roleTag : getRoleTags()) {
roleMap.put(roleTag.getRole(), roleTag.isGranting());
}
return roleMap;
}
private Map<Role, Boolean> getParentsRoleMap() {
return mergeRolesFromGroups(getParentGroups());
}
private Map<Role, Boolean> getMergedRoleMap() {
Map<Role, Boolean> roleMap = getRoleMap();
for (Entry<Role, Boolean> mergedEntry : getParentsRoleMap().entrySet()) {
// the roleMap override all the parents directives
if (!roleMap.containsKey(mergedEntry.getKey())) {
roleMap.put(mergedEntry.getKey(), mergedEntry.getValue());
}
}
return roleMap;
}
private static Map<Role, Boolean> mergeRolesFromGroups(List<SPGroup> groups) {
Map<Role, Boolean> mergedRoleMap = new HashMap<Role, Boolean>();
// merge of the parents' role
for (SPGroup group : groups) {
for (Entry<Role, Boolean> roleMapEntry : group.getMergedRoleMap().entrySet()) {
if (mergedRoleMap.containsKey(roleMapEntry.getKey())) {
// We apply a OR operation between the parents' role
if (roleMapEntry.getValue() && !mergedRoleMap.get(roleMapEntry.getKey())) {
mergedRoleMap.put(roleMapEntry.getKey(), roleMapEntry.getValue());
}
} else {
mergedRoleMap.put(roleMapEntry.getKey(), roleMapEntry.getValue());
}
}
}
return mergedRoleMap;
}
private static Set<Role> getActiveRoles(Map<Role, Boolean> roleMap) {
Set<Role> roles = new HashSet<Role>();
for (Entry<Role, Boolean> mapEntry : roleMap.entrySet()) {
if (mapEntry.getValue()) {
roles.add(mapEntry.getKey());
}
}
return roles;
}
private Map<SPPermission, SPPerimeter> getPermissionMap() {
Map<SPPermission, SPPerimeter> permissionMap = new HashMap<SPPermission, SPPerimeter>();
for (PermissionTag permissionTag : getPermissionTags()) {
if (!permissionMap.containsKey(permissionTag.getPermission())) {
permissionMap.put(permissionTag.getPermission(), new SPPerimeter());
}
permissionMap.get(permissionTag.getPermission()).add(permissionTag);
}
return permissionMap;
}
private Map<SPPermission, SPPerimeter> getParentsPermissionMap() {
return mergePermissionsFromGroups(getParentGroups());
}
public Map<SPPermission, SPPerimeter> getMergedPermissionMap() {
Map<SPPermission, SPPerimeter> permissionMap = getPermissionMap();
for (Entry<SPPermission, SPPerimeter> entry : getParentsPermissionMap().entrySet()) {
if (permissionMap.containsKey(entry.getKey())) {
permissionMap.get(entry.getKey()).merge(entry.getValue());
} else {
permissionMap.put(entry.getKey(), entry.getValue());
}
}
return permissionMap;
}
public static Map<SPPermission, SPPerimeter> mergePermissionsFromGroups(List<SPGroup> groups) {
Map<SPPermission, SPPerimeter> permissionMap = new HashMap<SPPermission, SPPerimeter>();
for (SPGroup group : groups) {
for (Entry<SPPermission, SPPerimeter> entry : group.getMergedPermissionMap().entrySet()) {
if (permissionMap.containsKey(entry.getKey())) {
permissionMap.get(entry.getKey()).add(entry.getValue());
} else {
permissionMap.put(entry.getKey(), entry.getValue());
}
}
}
return permissionMap;
}
@Override
public Map<SPPermission, SPPerimeter> getPermissions() {
return getMergedPermissionMap();
}
@Override
public String toString() {
return getName() + "[" + getId() + "]";
}
@Override
public void setRole(GrantedAuthority role, boolean granting) {
if (role instanceof Role) {
setRole((Role) role, granting);
} else {
setRole(new Role(role.getAuthority()), granting);
}
}
@Override
public void setPermission(Permission permission, Perimeter perimeter) {
if (permission instanceof SPPermission) {
setPermission((SPPermission) permission, perimeter);
} else {
setPermission(new SPPermission(permission.getObjectType(), permission.getAction()), perimeter);
}
}
private void setRole(Role role, boolean granting) {
Map<Role, Boolean> roleMap = getMergedRoleMap();
if (granting) {
if (!roleMap.containsKey(role)) {
// Role not defined
getRoleTags().add(new RoleTag(role, true));
} else if (!roleMap.get(role)) {
// Role explicitly forbidden
Iterator<RoleTag> iterator = getRoleTags().iterator();
while (iterator.hasNext()) {
RoleTag current = iterator.next();
if (current.getRole().equals(role)) {
iterator.remove();
}
}
getRoleTags().add(new RoleTag(role, true));
}
} else {
if (roleMap.containsKey(role) && roleMap.get(role)) {
// Role present
Iterator<RoleTag> iterator = getRoleTags().iterator();
boolean removed = false;
while (iterator.hasNext()) {
RoleTag current = iterator.next();
if (current.getRole().equals(role)) {
iterator.remove();
removed = true;
}
}
if (removed) {
// Role was present locally, check inheritance again
roleMap = getMergedRoleMap();
if (roleMap.containsKey(role) && roleMap.get(role)) {
// Role still present, explicit remove
getRoleTags().add(new RoleTag(role, false));
}
} else {
// Role was not present locally, explicit remove
getRoleTags().add(new RoleTag(role, false));
}
}
}
}
private boolean clearLocalPermission(SPPermission permission) {
Iterator<PermissionTag> iterator = getPermissionTags().iterator();
boolean modified = false;
while (iterator.hasNext()) {
PermissionTag permissionTag = iterator.next();
if (permissionTag.getPermission().equals(permission)) {
iterator.remove();
modified = true;
}
}
return modified;
}
private void addLimitedPermisisonTags(SPPermission permission, Perimeter perimeter, Perimeter inheritedPerimeter) throws IllegalArgumentException {
boolean isInherited = inheritedPerimeter != null;
if (perimeter.isMyAllowed()) {
if (!isInherited || !inheritedPerimeter.isMyAllowed()) {
addPermissionTag(new PermissionTag(permission, Type.MY));
}
}
for (Group group : perimeter.getAllowedGroups()) {
if (group instanceof SPGroup) {
if (!isInherited || !inheritedPerimeter.getAllowedGroups().contains(group)) {
addPermissionTag(new PermissionTag(permission, (SPGroup) group));
}
} else {
throw new IllegalArgumentException("Groups in perimeter must be an instance of com.sample.security.metier.impl.SPGroup");
}
}
for (User user : perimeter.getAllowedUsers()) {
if (user instanceof SPUser) {
if (!isInherited || !inheritedPerimeter.getAllowedUsers().contains(user)) {
addPermissionTag(new PermissionTag(permission, (SPUser) user));
}
} else {
throw new IllegalArgumentException("Users in perimeter must be an instance of com.sample.security.metier.impl.SPUser");
}
}
}
...
}
我的 junit Spring 上下文:test-context.xml
<beans ...>
<util:properties id="properties">
<prop key="SMPsecurity.dataSource">dataSource</prop>
<prop key="SMPsecurity.hibernate.generateDdl">false</prop>
<prop key="SMPsecurity.hibernate.target-database">HSQL</prop>
</util:properties>
<context:property-placeholder properties-ref="properties"/>
<import resource="classpath*:spring/SMPsecurity-context.xml" />
<jdbc:embedded-database id="dataSource" type="HSQL" >
<jdbc:script location="classpath:database/create-schemas.sql"/>
</jdbc:embedded-database>
<jdbc:initialize-database data-source="dataSource" >
<jdbc:script location="classpath:database/populate-security-data.sql" />
</jdbc:initialize-database>
....
</beans>
上下文的附加文件:SMPsecurity-context.xml
<beans ...>
<tx:annotation-driven transaction-manager="transactionManagerSMPSecurity" />
<jpa:repositories base-package="com.sample.security.repositories" entity-manager-factory-ref="entityManagerFactorySMPSecurity" transaction-manager-ref="transactionManagerSMPSecurity"/>
<util:map id="HibernateConfigurationSMPSecurity">
<entry key="hibernate.generateDdl" value="${SMPsecurity.hibernate.ddl-generation:true}" />
<entry key="hibernate.databasePlatform" value="org.hibernate.dialect.${SMPsecurity.hibernate.target-database:HSQL}Dialect" />
<entry key="hibernate.hbm2ddl.auto" value="${SMPsecurity.hibernate.hbm2ddl:validate}"/>
</util:map>
<bean id="entityManagerFactorySMPSecurity" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="${SMPsecurity.dataSource:dataSourceSMPSecurity}"/>
<property name="packagesToScan" value="com.sample.security.metier.impl" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
<property name="showSql" value="false"/>
</bean>
</property>
<property name="persistenceXmlLocation" value="classpath:data/security/security-persistence.xml"/>
<property name="jpaPropertyMap" ref="HibernateConfigurationSMPSecurity"/>
</bean>
<bean id="transactionManagerSMPSecurity" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactorySMPSecurity" />
</bean>
</beans>
和数据库的架构:
CREATE schema security AUTHORIZATION DBA;--déclaration du schema et des droits pour HSQL
CREATE TABLE security.group (grp_id BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL, grp_name VARCHAR(150) NOT NULL, PRIMARY KEY (grp_id));
REATE TABLE security.user (usr_id BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL, usr_creation TIMESTAMP, usr_email VARCHAR(128), usr_enabled BOOLEAN NOT NULL, usr_expiration TIMESTAMP, usr_firstname VARCHAR(50), usr_lastname VARCHAR(50), usr_modification TIMESTAMP, usr_password VARCHAR(100) NOT NULL, usr_login VARCHAR(128) NOT NULL, PRIMARY KEY (usr_id));
CREATE TABLE security.group_user (grp_id BIGINT NOT NULL, usr_id BIGINT NOT NULL, PRIMARY KEY (grp_id, usr_id));
ALTER TABLE security.group ADD CONSTRAINT UNQ_group_1 UNIQUE (grp_name);
ALTER TABLE security.user ADD CONSTRAINT UNQ_user_2 UNIQUE (usr_email);
ALTER TABLE security.user ADD CONSTRAINT UNQ_user_9 UNIQUE (usr_login);
ALTER TABLE security.group_user ADD CONSTRAINT FK_group_user_usr_id FOREIGN KEY (usr_id) REFERENCES security.user (usr_id);
ALTER TABLE security.group_user ADD CONSTRAINT FK_group_user_grp_id FOREIGN KEY (grp_id) REFERENCES security.group (grp_id);
这是单元测试
@ContextConfiguration(value = "classpath:spring/test-context.xml")
@Transactional
public class SecurityTest {
@Autowired
private UserRepository userRepository;
@Autowired
private GroupRepository groupRepository;
...
private void setCurrentUser(User user) {
Authentication authentication = new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
}
@Test
public void roleCommercialTest() {
SPUser commercial_63_1 = userRepository.findOne(1L);
setCurrentUser(commercial_63_1);
Assert.assertTrue(SMPPubServiceOrController.testHasAction1());
}
...
}
最佳答案
我找到了。我必须像这样在 ManyToMany 注释上添加模式:
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "group_user", schema="security", joinColumns = {
@JoinColumn(name = "grp_id", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "usr_id",
nullable = false, updatable = false) })
关于java - JPA + Spring 4 + hibernate 4 : user lacks privilege or object not found : USER_GROUP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23381112/
我有以下情况要解决,但无法正常工作(尝试了Hibernate和EclipseLink): Table_1: Column_A is Primary Key ... some other
我是 JPA 的新手,但必须在该技术中实现我的项目 我想做的是通过 CriteriaQuery 构建一些数据库查询,但不知道如何将参数列表传递给下面的代码: CriteriaBuilder qb =
我是 JPA 新手,注意到可以通过使用 @Version 注释实体中的字段来使用乐观锁定。我只是好奇,如果之前不存在,持久性提供程序是否会创建一个隐式版本字段。例如网站objectdb状态: "Whe
我有一个 JPA 查询 @Query(value = "SELECT SUM(total_price) FROM ... WHERE ...", nativeQuery = true) 当有匹配的记录
JPA 是否会尝试在已经持久(和非分离)的实体上级联持久化? 为了清楚起见,这是我的情况:我想保留一个新用户: public void addUser(){ //User is an enti
显然,OpenJPA。我也看到提到过 EclipseLink 和 Hibernate,但是在功能上有显着差异吗? 最佳答案 大多数差异来自提供者对 OSGi 的感知程度。例如,您可能需要自己将 Hib
我想将 JPA 用于 micronaut。为此,我使用 io.micronaut.data:micronaut-data-hibernate-jpa:1.0.0.M1 库。每当我运行应用程序并点击端点
我正准备为我的应用实现后端,现在我正在投影数据层。我期待着 Spring 。 最佳答案 Spring Data JPA 不是 JPA 实现。它提供了将数据访问层构建到底层 JPA 顶部的方法。您是否应
假设我有一个表 Item,其中包含一个名为 user_id 的列和一个表 User 以及另一个名为 Superuser 的列: CREATE TABLE Item(id int, user_id in
JPA 2.1 规范说: The entity class must not be final. No methods or persistent instance variables of the
我正在从事一个具有一些不寻常实体关系的项目,我在使用 JPA 时遇到了问题。有两个相关对象;用户,让我们称另一个 X。用户与 X 具有一对多和两个一对一的关系。它基本上看起来像这样 [用户实体] @O
我说的是 JavaEE 中的 JPA。在我读过的一本书中谈到: EntityManager em; em.find(Employee.class, id); “这是实体管理器在数据库中查找实例所需的所
我有 JPA 支持的 Vaadin 应用程序。此应用程序中的组件绑定(bind)到 bean 属性(通过独立的 EL 实现)。一些组件绑定(bind)到外部对象(或其字段),由@OneToOne、@O
是否可以使表中的外键唯一?假设我有实体 A 和 B。 答: @Entity class A extends Serializable { @Id private long id; @OneToOne
我在使用 JPA 时遇到了一点问题。考虑这种情况: 表 A (id_a) | 表 B (id_b, id_a) 我需要的是这样的查询: Select a.*, c.quantity from A as
我有一个由 JPA 管理的实体类,我有一个实体需要在其属性中记录更改。 JPA 是否提供任何方法来处理这种需求? 最佳答案 如果您使用 Hibernate 作为 JPA 提供程序,请查看 Hibern
我想实现以下架构: Table A: a_id (other columns) Table B: b_id (other columns) Table C: c_id (other columns)
我有一个愚蠢的问题。如果能做到的话那就太好了,但我并没有屏住呼吸。 我需要链接到我的 JPA 实体的表中的单个列作为所述 JPA 实体中的集合。有什么方法可以让我单独取回与该实体相关的列,而不必取回整
我有一个 Open JPA 实体,它成功连接了多对多关系。现在我成功地获取了整个表,但我实际上只想要该表中的 ID。我计划稍后调用数据库来重建我需要的实体(根据我的程序流程)。我只需要 ID(或该表中
这是我的一个实体的复合主键。 public class GroupMembershipPK implements Serializable{ private static final long
我是一名优秀的程序员,十分优秀!