我正在使用Spring MVC/Security/JPA/Hibernate开发一个web应用程序。
我有一个用户类,它与UserRole有一个omany关系。
@Entity
@Table(name = "user_accounts")
@SuppressWarnings("serial")
public class User extends SimpleBaseEntity<Long> implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "email", length = 100, nullable = false, unique = true)
private String email;
@Column(name = "first_name", length = 100,nullable = false)
private String firstName;
@Column(name = "last_name", length = 100, nullable = false)
private String lastName;
@Column(name = "password", length = 255)
private String password;
@OneToMany(fetch = FetchType.EAGER, cascade={CascadeType.ALL}, mappedBy="user")
@NotFound(action=NotFoundAction.IGNORE)
private List<UserRole> roles;
@Enumerated(EnumType.STRING)
@Column(name = "sign_in_provider", length = 20)
private SocialMediaService signInProvider;
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "user_image", length=16777216)
private byte[] userImage;
... skip
}
UserRole类与用户之间有很多关系。
@Entity
@Table(name = "user_roles")
@SuppressWarnings("serial")
public class UserRole implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, unique = true)
private Long id;
@Column(name = "user_id", length = 100, nullable = true)
private String userId;
@Enumerated(EnumType.STRING)
@Column(name = "role", length = 20, nullable = false)
private Role role;
@ManyToOne
@JoinColumn(name="user_id", referencedColumnName="email", insertable=false, updatable=false, nullable=false)
@NotFound(action=NotFoundAction.IGNORE)
private User user;
... skip
}
用户存储库界面非常简单。
public interface UserRepository extends JpaRepository<User, Long> {
public User findByEmail(String email);
}
当我使用下面的代码片段使用存储库加载用户信息时,它会很好地检索用户和用户角色数据。
User user = userRepository.findByEmail("some@email.com"); // OK both User and UserRole
这是日志文件。
Hibernate:
select
user0_.id as id1_9_,
user0_.creation_time as creation2_9_,
user0_.modification_time as modifica3_9_,
user0_.email as email4_9_,
user0_.first_name as first_na5_9_,
user0_.last_name as last_nam6_9_,
user0_.password as password7_9_,
user0_.sign_in_provider as sign_in_8_9_,
user0_.user_image as user_ima9_9_
from
user_accounts user0_
where
user0_.email=?
Hibernate:
select
roles0_.user_id as user_id3_9_1_,
roles0_.id as id1_10_1_,
roles0_.id as id1_10_0_,
roles0_.role as role2_10_0_,
roles0_.user_id as user_id3_10_0_
from
user_roles roles0_
where
roles0_.user_id=?
Hibernate:
select
user0_.id as id1_9_1_,
user0_.creation_time as creation2_9_1_,
user0_.modification_time as modifica3_9_1_,
user0_.email as email4_9_1_,
user0_.first_name as first_na5_9_1_,
user0_.last_name as last_nam6_9_1_,
user0_.password as password7_9_1_,
user0_.sign_in_provider as sign_in_8_9_1_,
user0_.user_image as user_ima9_9_1_,
roles1_.user_id as user_id3_9_3_,
roles1_.id as id1_10_3_,
roles1_.id as id1_10_0_,
roles1_.role as role2_10_0_,
roles1_.user_id as user_id3_10_0_
from
user_accounts user0_
left outer join
user_roles roles1_
on user0_.email=roles1_.user_id
where
user0_.email=?
然而,问题出在这里。
如果我使用UserRepository的save(merge)方法更新服务类中的用户信息,那么相同的findByEmail方法不会检索UserRole(roles)信息,而只检索用户信息。
User oldUser = userRepository.findByEmail("some@email.com"); // OK both User and UserRole(roles)
// ... some modifications of "oldUser"
userService.updateUser(oldUser); // This is Transactional and the table record is updated OK.
User newUser = userRepository.findByEmail("some@email.com"); // UserRole(roles) data is null
Hibernate日志文件在下面。
# findByEmail oldUser
Hibernate:
select
user0_.id as id1_9_,
user0_.creation_time as creation2_9_,
user0_.modification_time as modifica3_9_,
user0_.email as email4_9_,
user0_.first_name as first_na5_9_,
user0_.last_name as last_nam6_9_,
user0_.password as password7_9_,
user0_.sign_in_provider as sign_in_8_9_,
user0_.user_image as user_ima9_9_
from
user_accounts user0_
where
user0_.email=?
Hibernate:
select
roles0_.user_id as user_id3_9_1_,
roles0_.id as id1_10_1_,
roles0_.id as id1_10_0_,
roles0_.role as role2_10_0_,
roles0_.user_id as user_id3_10_0_
from
user_roles roles0_
where
roles0_.user_id=?
Hibernate:
select
user0_.id as id1_9_1_,
user0_.creation_time as creation2_9_1_,
user0_.modification_time as modifica3_9_1_,
user0_.email as email4_9_1_,
user0_.first_name as first_na5_9_1_,
user0_.last_name as last_nam6_9_1_,
user0_.password as password7_9_1_,
user0_.sign_in_provider as sign_in_8_9_1_,
user0_.user_image as user_ima9_9_1_,
roles1_.user_id as user_id3_9_3_,
roles1_.id as id1_10_3_,
roles1_.id as id1_10_0_,
roles1_.role as role2_10_0_,
roles1_.user_id as user_id3_10_0_
from
user_accounts user0_
left outer join
user_roles roles1_
on user0_.email=roles1_.user_id
where
user0_.email=?
# Update User
update
user_accounts
set
modification_time=?,
email=?,
first_name=?,
last_name=?,
password=?,
sign_in_provider=?,
user_image=?
where
id=?
# findByEmail newUser
Hibernate:
select
user0_.id as id1_9_,
user0_.creation_time as creation2_9_,
user0_.modification_time as modifica3_9_,
user0_.email as email4_9_,
user0_.first_name as first_na5_9_,
user0_.last_name as last_nam6_9_,
user0_.password as password7_9_,
user0_.sign_in_provider as sign_in_8_9_,
user0_.user_image as user_ima9_9_
from
user_accounts user0_
where
user0_.email=?
有人能帮我吗?
提前感谢。
最后,我找到了缺少关系列表数据的原因。
这是我设置事务的错误。
“updateUser(oldUser)”方法不是事务性的。
在我将其更改为事务性后,一切正常。
User oldUser = userRepository.findByEmail("some@email.com"); // OK both User and UserRole(roles)
// ... some modifications of "oldUser"
userService.updateUser(oldUser); // This was not Transactional and it was the reason of the problem.
User newUser = userRepository.findByEmail("some@email.com"); // UserRole(roles) data is null
问题内容: 请帮助我得到一个where 本身就是一个泛型类型。如我现在所见,Spring RestTemplate现在不支持此功能。我正在使用Spring MVC版本3.1.2 这是我要使用的代码:代码: 我收到此错误: 这是明显的错误,但是今天如何解决呢? 比我想得到我的通用响应类型: 现在,我使用此解决方案,并且不使用: 问题答案: 通过引入修复了该问题,你可以显式继承该参数化类型,以便在运行
我试图创建一个程序,在这个程序中我可以接受输入,并用servlet获得一个数字的平方根。我是初学者,所以知道的不多。问题是当我尝试我的代码时,它不起作用。代码如下: myservletdemo.java 结果是:首先,当你点击链接时,你会得到: 这个结果
在尝试执行GET到发布者存储库时,我正在执行GET和无限循环。 出版商: 书: 完整代码可在此处获得: https://github.com/vanyasav/library
问题内容: 我有一个里面。我已经注册了一个,我想以此为基础更新。我遇到的问题是我无法集中精力于,因此无法正常工作。我已经知道可以使用了,因为我在上注册了它,并且效果很好。目前,我的代码如下所示: 有人遇到过这样的问题吗?在这方面我缺少什么吗? PS:我内部没有任何组件,我只是在背景上绘制图像,因此我需要将焦点放在JPanel本身而不是JPanel上。 问题答案: 尽管您指出面板可以对焦,但是面板并
问题内容: 如果我在下面的代码中将type设置为’GET’,则可以使用,但无法将其与’POST’一起使用。 ajaxPostTest.html … ajaxPostTest_server.php … 浏览器上的预期输出为… 我实际上是在尝试与ASP.NET Web服务器进行交互,并且我想先确认我的ajax是否可以正常工作(并且我对ajax的理解正确),因此可以简化此代码。 Firebug调试… 我
问题内容: 我有线程池,它将带一个工作线程。我需要获得此Callable任务,但无法获得它。 在下面的示例中,我需要为RejectionHandler执行的Callable任务的uniqueId。在中,浇铸,我希望它应该被强制转换为工作线程。 请帮助我获取中的Worker线程实例。 输出量 我期待CallableWorkerThread而不是FutureTask。帮助我获取WorkerThread