小编sha*_*ltc的帖子

JPA OneToMany:List vs Set

我有两个实体:UserAccountNotification.这些关系如下所示.

 public class UserAccount {

    @Id
    @Column(name = "USER_NAME")
    private String emailId;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "USERS_NOTIFICATIONS", joinColumns = { @JoinColumn(name = "USER_NAME") }, inverseJoinColumns = { @JoinColumn(name = "NOTIFICATION_ID") })
    private List<Notification> notifications;

    //setters, getter, equals and hashcode
 }
Run Code Online (Sandbox Code Playgroud)

equals()hashcode()被覆盖(通过与业务键/主键IDE生成).

给定a UserAccount,当我添加第一个时Notification,它会产生一个INSERT语句.但是在进一步添加时UserAccount,它首先删除然后插入:

Hibernate: delete from USERS_NOTIFICATIONS where USER_NAME=?
Hibernate: insert into USERS_NOTIFICATIONS (USER_NAME, NOTIFICATION_ID) values (?, ?)
Hibernate: insert into USERS_NOTIFICATIONS (USER_NAME, …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa

19
推荐指数
2
解决办法
2万
查看次数

@embeddable vs @entity用于映射集合

这一定是天真的,但我怀疑何时使用@Entity@Embeddable.

说我有一个User和一个Notification班级.

 @Entity
 public class User{
     //other properties
     @onetomany
     private List<Notification> notifications;
 }

 @Entity
 public class Notification{
     //properties
 }
Run Code Online (Sandbox Code Playgroud)

据我所知,将有类表UserNotification,以及映射第三个表.如果我这样做怎么办?

 @Entity
 public class User {
     //other properties
     @ElementCollection
     private List<Notification> notifications;
 }

 @Embeddable
 public class Notification{
     //properties
 }
Run Code Online (Sandbox Code Playgroud)

我知道这不会创建一个表Notification.但我仍然可以存储我的通知对象.我浏览了文档,但有些疑惑:

  1. 是否基于我是否希望将B类视为单独的表格?
  2. 是否存在创建表和可嵌入对象的性能差异?
  3. 除了直接查询表之外,我可以用除了可嵌入对象之外的其他对象做什么?

笔记

对于读这个问题的人来说,这个问题也可能对你有帮助.

java orm spring hibernate jpa

17
推荐指数
1
解决办法
2万
查看次数

密码:IllegalBlockSizeException的原因是什么?

当我使用Cipher时,我观察到以下内容.

加密代码:

Cipher aes = Cipher.getInstance("AES");
aes.init(Cipher.ENCRYPT_MODE, generateKey());
byte[] ciphertext = aes.doFinal(rawPassword.getBytes());
Run Code Online (Sandbox Code Playgroud)

解密代码:

Cipher aes = Cipher.getInstance("AES");
aes.init(Cipher.DECRYPT_MODE, generateKey());
byte[] ciphertext = aes.doFinal(rawPassword.getBytes());
Run Code Online (Sandbox Code Playgroud)

在运行Decrypt代码时,我得到IllegalBlockSizeException(输入长度​​必须是16的倍数).

但是,如果我将解密代码更改为

Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding"); //I am passing the padding too
aes.init(Cipher.DECRYPT_MODE, generateKey());
byte[] ciphertext = aes.doFinal(rawPassword.getBytes());
Run Code Online (Sandbox Code Playgroud)

它工作正常.我明白这是在模式中algorithm/mode/padding.所以我认为这是因为我没有提到填充.所以我尝试在加密过程中给出模式和填充,

加密代码:

Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");//Gave padding during encryption too
aes.init(Cipher.ENCRYPT_MODE, generateKey());
byte[] ciphertext = aes.doFinal(rawPassword.getBytes());
Run Code Online (Sandbox Code Playgroud)

解密代码:

Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
aes.init(Cipher.DECRYPT_MODE, generateKey());
byte[] ciphertext = aes.doFinal(rawPassword.getBytes());
Run Code Online (Sandbox Code Playgroud)

但它失败了IllegalBlockSizeException.

是什么原因,为什么例外以及究竟发生了什么.如果有人可以帮忙吗?提前致谢

UPDATE

看起来问题在于我正在加密和解密的字符串.因为,即使我说的代码有效,也并不总是有效.我基本上是在加密UUID(例如:8e7307a2-ef01-4d7d-b854-e81ce152bbf6).它适用于某些字符串,而不适用于某些字符串.

加密String的长度为64,可被16整除.是的,我在同一台机器上运行它.

密钥生成方法:

    private Key …
Run Code Online (Sandbox Code Playgroud)

java encryption aes

17
推荐指数
2
解决办法
1万
查看次数

spring security中的手动认证逻辑应该放在哪里 - 服务层或表示层?

我有这段代码

UserDetails userDetails = userDetailsServiceImpl.loadUserByUsername(email);
Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
SecurityContext securityContext = SecurityContextHolder.getContext();
securityContext.setAuthentication(authentication);
HttpSession session = request.getSession(true);
session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);
Run Code Online (Sandbox Code Playgroud)

这是在spring security中手动验证用户身份.我的问题是我应该在哪里放置此代码?把它放在服务层强制我将HttpSession对象带到AFAIK不好的服务层.我不确定将认证逻辑放在表示层中有多好.任何有见解的人?

提前致谢.

java spring spring-security

7
推荐指数
1
解决办法
6756
查看次数

Spring Social - Facebook登录404错误

我在我的应用程序中有春季安全登录的春季3.1.我正在尝试使用spring social添加facebook登录.它进入Facebook登录页面,但登录后它会引发404错误.我在URL中有这个:

http://localhost:8080/TestProject/signin?error=provider#_=_
Run Code Online (Sandbox Code Playgroud)

这是我的春季社交配置:

<bean class="org.springframework.social.connect.web.ProviderSignInController">
<!-- relies on by-type autowiring for the constructor-args -->    
<constructor-arg ref="signInAdapter" />
</bean>

<bean id="connectionFactoryLocator" 
  class="org.springframework.social.connect.support.ConnectionFactoryRegistry">
<property name="connectionFactories">
    <list>
        <bean class="org.springframework.social.facebook.connect.FacebookConnectionFactory">
            <constructor-arg value="${fb.id}" />
            <constructor-arg value="${fb.passwrd}" />               
        </bean>
    </list>
</property>
</bean>

<bean id="connectionRepository" factory-method="createConnectionRepository" 
  factory-bean="usersConnectionRepository" scope="request">
<constructor-arg value="#{request.userPrincipal.name}" />
<aop:scoped-proxy proxy-target-class="false" />
</bean>

<bean id="signInAdapter" class="com.Test.social.SimpleSignInAdapter"/>

<bean id="usersConnectionRepository" 
  class="org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository">
<constructor-arg ref="dataSource" />
<constructor-arg ref="connectionFactoryLocator" />
<constructor-arg ref="textEncryptor" />
</bean>

<bean id="textEncryptor" class="org.springframework.security.crypto.encrypt.Encryptors" 
        factory-method="noOpText" />
 </beans> 
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

spring spring-mvc spring-security facebook-oauth spring-social

6
推荐指数
1
解决办法
2473
查看次数

原始类型和数据完整性列表

在这个例子中

public static void main(String[] args) {

    List<Integer> integers = new ArrayList<Integer>();
    integers.add(1);
    addToList(integers);
    System.out.println(integers);
}


public static void addToList(List list0){
    list0.add("blabl");
}
Run Code Online (Sandbox Code Playgroud)

这将编译并打印结果

[1,blabl]

我的理解是:

引用变量'integers'具有arraylist对象的地址(比如111),该地址被传递给addToList方法.因此在addToList方法中,list0指向具有该对象的同一地址(它是Integer类型的arraylist),并且将一个字符串添加到此arraylist对象中.

如何将String添加到Integer类型的arraylist?这不是数据完整性问题吗?

更新

下面的答案和这个答案有帮助.谢谢.

java generics arraylist

6
推荐指数
1
解决办法
101
查看次数

Spring @ExceptionHandler没有捕获AccessDeniedException

我有一个使用以下代码设置的异常处理程序

@ExceptionHandler(Throwable.class)
public @ResponseBody CustomResponse handleException(Throwable throwable){
    // handles exception, returns a JSON
}
Run Code Online (Sandbox Code Playgroud)

我正在使用Spring Security 3.1.当我尝试在没有身份验证的情况下执行操作时,应用程序会抛出AccessDeniedException.它永远不会出现这种方法.但其他例外可以正常工作.这是应该工作的方式吗?或者我的代码有问题吗?

看起来像一个解决方案.但如果我能在一个点处理异常会更好.

这是我的配置文件

<global-method-security secured-annotations="enabled" pre-post-annotations="enabled" />
<http auto-config="true" use-expressions="true">
    //intercept urls
    <form-login login-page="/signin" default-target-url="/" always-use-default-target="true"/>
</http>

<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService">
        <password-encoder ref="encoder" />
    </authentication-provider>
</authentication-manager>

<!-- This encoder doesn't require a salt -->
<beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />
Run Code Online (Sandbox Code Playgroud)

UPDATE

这里用户未经过身份验证(ROLE_ANONYMOUS).当我尝试访问受保护的页面时,它会将我重定向到登录URL.我的问题在于,我进行了一次AJAX调用.因此,重定向到返回ModelAndView的方法不起作用.这附近有工作吗?

java spring spring-mvc spring-security

5
推荐指数
1
解决办法
6202
查看次数

xmlns定义在spring配置文件中有何不同

学习Spring我在spring配置文件中找到了两种类型的xmlns定义.一个从这开始:

 <beans xmlns="http://www.springframework.org/schema/beans"
Run Code Online (Sandbox Code Playgroud)

这是我在spring 文档中找到的

而另一个从这开始:

 <beans:beans  xmlns="http://www.springframework.org/schema/mvc"
Run Code Online (Sandbox Code Playgroud)

两者都很好.我观察到的一个区别是,如果使用第二个定义,则必须使用名称bean启动所有标记,如下所示:

<beans:import resource="hibernate-context.xml" /> 
Run Code Online (Sandbox Code Playgroud)

否则可以写成

 <import resource="hibernate-context.xml" />
Run Code Online (Sandbox Code Playgroud)

他们有什么主要区别?

spring spring-mvc

4
推荐指数
1
解决办法
2126
查看次数

JPA:删除时的外键约束

我有以下关系

Class UserAccount{
//Other fields
@OneToMany(mappedBy = "userAccount", cascade = CascadeType.REMOVE)
private Set<Images> imagesShared;

@ManyToMany
@JoinTable(name = "USER_LIKES", joinColumns = @JoinColumn(name = "USER_NAME"),   inverseJoinColumns = @JoinColumn(name = "ID"))
private Set<Images> imagesLiked;
}

Class Images{
//other fields
@ManyToMany(mappedBy = "imagesLiked")
private Set<UserAccount> likes;
}
Run Code Online (Sandbox Code Playgroud)

这些线后我得到了一个例外

Hibernate: delete from IMAGES where ID=?
Hibernate: delete from COMMENTS where COMMENT_ID=?
Hibernate: delete from COMMENTS where COMMENT_ID=?
Hibernate: delete from COMMENTS where COMMENT_ID=?
Hibernate: delete from COMMENTS where COMMENT_ID=?
Hibernate: delete from COMMENTS where COMMENT_ID=? …
Run Code Online (Sandbox Code Playgroud)

java mysql hibernate jpa

4
推荐指数
1
解决办法
2万
查看次数

Spring 3.1-找不到javascript文件 - 404错误

我在文件夹WebContent/resources/js/test.js中有一个js文件.我试图在jsp中包含相同的文件.但是jsp文件无法找到js文件(浏览器控制台中出现404错误).我在SO中抛出了几个问题:

可以将SpringMVC配置为处理所有请求,但排除静态内容目录吗?

在调用转发到JSP的Servlet时,浏览器无法访问/查找CSS,图像和链接等相关资源

STS Spring MVC:如何在JSP中包含JS文件

但仍然没有帮助.这是我的代码:

在应用程序上下文中,我使用的是mvc:resource标记.

<mvc:resources mapping="/resources/**" location="/resources/" />
Run Code Online (Sandbox Code Playgroud)

在我的jsp中

<script src="${contextPath}/resources/js/test.js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)

尝试给src值

 <c:url> 
Run Code Online (Sandbox Code Playgroud)

太.

我的web.xml有

<servlet-mapping> 
<servlet-name>TestProject</servlet-name> 
<url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

萤火虫说

The requested resource (/resources/js/test.js) is not available.
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

UPDATE

firebug中的GET请求URL就是这个

http://localhost:8080/TestProject/resources/js/test.js
Run Code Online (Sandbox Code Playgroud)

这样对吗??

javascript spring jsp spring-mvc

3
推荐指数
1
解决办法
1万
查看次数

Jquery .text()不起作用

我有这样的锚标记:

<a id="1_18_shazin@mycompany.com" class="big-link" href="">4</a>
Run Code Online (Sandbox Code Playgroud)

现在我尝试用以下代码替换文本

a = 1;
b = 18;
c = "shazin@mycompany.com";
var tempId = "#"+a+"_"+b+"_"+c;
$(tempId).text("some text");
Run Code Online (Sandbox Code Playgroud)

Thiis永远不会奏效.我试过.html(),. append().看起来它永远不会找到ID.但是如果我按类名调用事件,它就有效.

$(".big-link").text("some text");
Run Code Online (Sandbox Code Playgroud)

任何线索?提前致谢.

html javascript jquery

1
推荐指数
1
解决办法
2万
查看次数