我阅读了很多程序员的帖子,这些帖子遇到了Unable,以确定依赖操作的有效排序.在实体框架中使用自引用关系时,由于外键约束,模型要求或存储生成的值 -例外,可能存在依赖关系.
我正在努力建立一个亲子关系:
public class Category {
public int CategoryId { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
public Category Parent { get; set; }
public List<Category> Children { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我使用的配置(Fluent API):
Property(c => c.ParentId).IsOptional();
HasMany(c => c.Children).WithOptional(c => c.Parent).HasForeignKey(c => c.ParentId);
//HasOptional(c => c.Parent).WithMany(c => c.Children).HasForeignKey(c => c.ParentId);
Run Code Online (Sandbox Code Playgroud)
当我尝试保存这样的新类别时,HasMany()和HasOptional()配置都会导致"无法确定依赖操作的有效排序..."异常:
context.Categories.Add(new Category { Name = "test" });
Run Code Online (Sandbox Code Playgroud)
我不明白为什么EF不插入带有null parentId的Category.数据库允许ParentId外键为null.
你能告诉我怎么做吗?
entity-framework parent-child self-reference ef-code-first entity-framework-4.1
这可能是关于这个Spring MVC错误的第100个问题,但是我无法让它继续工作.
我正在尝试将一个简单的控制器方法映射到/ account,稍后我想添加/ account/{id},但我甚至无法使用/ account工作.
这是我的web.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>My Spring MVC web application</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
</web-app>
Run Code Online (Sandbox Code Playgroud)
application-context.xml的内容:
<mvc:annotation-driven />
<context:component-scan base-package="org.example" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
AccountController.java:
@Controller
public class AccountController {
@RequestMapping(value="/account", method = RequestMethod.GET)
public ModelAndView showAccount() throws Exception {
ModelAndView mav = new ModelAndView(); …Run Code Online (Sandbox Code Playgroud) 我昨天工作了,然后我做了一些事情,现在我一直在努力修复它几个小时,我再也无法让它工作了.
我有一个Spring MVC应用程序包含一个<form:form>我想<form:errors>在用户键入错误信息时从.properties文件显示自定义错误消息().在JSR-303注释中定义了什么'错误'.
从表格中摘录:
<form:form method="post" action="adduserprofile" modelAttribute="bindableUserProfile">
<table>
<tr>
<td><form:label path="firstName">Voornaam</form:label></td>
<td>
<form:input path="firstName"/>
<form:errors path="firstName" />
</td>
</tr>
<tr>
<td><form:label path="lastName">Achternaam</form:label></td>
<td>
<form:input path="lastName"/>
<form:errors path="lastName" />
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
摘自BindableUserProfile:
@NotNull
@Size(min = 3, max = 40, message="{errors.requiredfield}")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@NotNull
@Size(min = 3, max = 40, message="errors.requiredfield")
public String getLastName() {
return lastName;
}
Run Code Online (Sandbox Code Playgroud)
摘录自控制器:
@RequestMapping(value = "/edit/{userProfileId}", method …Run Code Online (Sandbox Code Playgroud) 我正在使用Bootstrap 3网格,我希望不适合该行的单元格被父级溢出隐藏:隐藏的CSS属性,而不是由Bootstrap("堆叠")显示在下一行.
这可能吗?请看一下这个例子:
http://plnkr.co/edit/TYyEcYSe1MhZWTCFnJln?p=preview
<!DOCTYPE html>
<html>
<head>
<style>
#grid-container {
overflow: hidden;
}
#grid-container div {
background-color: #cdcdcd;
border-right: 1px solid white;
border-bottom: 1px solid white;
}
</style>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div id="grid-container" class="col-xs-12">
<div class="col-xs-6 col-sm-4 col-md-3 col-lg-2">
Item 1
</div>
<div class="col-xs-6 col-sm-4 col-md-3 col-lg-2">
Item 2
</div>
<div class="col-xs-6 col-sm-4 col-md-3 col-lg-2">
Item 3
</div>
<div class="col-xs-6 col-sm-4 col-md-3 col-lg-2">
Item 4
</div> …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring安全性对用户进行身份验证。我创建了一个自定义身份验证提供程序,现在我想知道如何将错误消息从提供程序获取到表单中。这是我的自定义身份验证提供程序中的authenticate()方法:
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
UserProfile profile = userProfileService.findByEmail(authentication.getPrincipal().toString());
if(profile == null){
throw new UsernameNotFoundException(String.format("Invalid credentials", authentication.getPrincipal()));
}
String suppliedPasswordHash = DigestUtils.shaHex(authentication.getCredentials().toString());
if(!profile.getPasswordHash().equals(suppliedPasswordHash)){
throw new BadCredentialsException("Invalid credentials");
}
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(profile, null, profile.getAuthorities());
return token;
}
Run Code Online (Sandbox Code Playgroud)
这是我的表格:
<form name='f' action="<c:url value='j_spring_security_check' />" method='POST'>
<div id="data-entry-form">
<div class="form-entry">
<label><spring:message code="login.form.label.email"/></label>
<input type='text' name='j_username' value=''>
</div>
<div class="form-entry">
<label><spring:message code="login.form.label.password"/></label>
<input type='password' name='j_password'/>
</div>
<div class="form-entry">
<input type="submit" value="Verzenden"/>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
如何将错误消息发送到表单中?从我按下登录按钮的那一刻起,Spring接管了工作,因此我可以生成错误消息的唯一方法是authenticate()方法。
我正在尝试更新嵌入式实体,并且JPA似乎生成了错误的SQL。
我有一个带有嵌入式徽标实体的公司实体
@Entity
public class Company {
private Long id;
@Embedded
private Logo logo;
// Omitted other fields, getters, setters, etc
}
@Embeddable
public class Logo {
private String fileName;
private String fileExtension;
private String imageDataType;
// Omitted getters and setters
}
Run Code Online (Sandbox Code Playgroud)
在我的DAO方法中,我试图像这样更新嵌入式徽标:
@Override
public void setLogo(Logo logo, Long companyId) {
String q = "update Company c SET c.logo = :logo where c.id = :companyId";
Query query = entityManager.createQuery(q);
query.setParameter("companyId", companyId);
query.setParameter("logo", logo);
query.executeUpdate();
}
Run Code Online (Sandbox Code Playgroud)
JPA(实际上是Hibernate)生成以下SQL。
update px_company set file_extension, …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我必须从主应用程序线程异步处理多个作业并收集每个作业的结果.我有一个普通的Java解决方案,它使用ExecutorService和ExecutorCompletionService来收集作业结果.
现在我想将我的代码转换为Spring解决方案.该文档显示我使用的ExecutorService和@Async注释,但如何,我不知道如何,如果能收集多个作业的结果.
换句话说:我正在寻找与CompletionService相当的Spring.有这样的事吗?
我目前的代码:
class MyService {
private static ExecutorService executorService;
private static CompletionService<String> taskCompletionService;
// static init block
static {
executorService = Executors.newFixedThreadPool(4);
taskCompletionService = new ExecutorCompletionService<String>(executorService);
// Create thread that keeps looking for results
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Future<String> future = taskCompletionService.take();
String s = future.get();
LOG.debug(s);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}).start();
}
// This …Run Code Online (Sandbox Code Playgroud) 我已经阅读了很多关于级联和多对多关联的主题,但我无法找到我的特定问题的答案.
我在UserProfiles和Roles之间有多对多的关系.当我删除UserProfile时,我希望数据库删除连接表(userprofile2role)中的关联记录,因此使用实际的SQL"ON DELETE CASCADE"操作.这可能吗?无论我尝试什么,Hibernate总是创建UserProfile表而不指定ON DELETE行为.
UserProfile映射:
@Entity
public class UserProfile {
private Long id;
private Set<Role> roles;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public final Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
// Note: CascadeType.ALL doesn't work for many-to-many relationships
@ManyToMany (fetch = FetchType.EAGER)
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
Run Code Online (Sandbox Code Playgroud)
角色映射:
@Entity
public class Role {
private Long id;
private Set<UserProfile> userProfiles = …Run Code Online (Sandbox Code Playgroud) spring ×4
bundle ×1
cascade ×1
entity ×1
grid ×1
hibernate ×1
jpa-2.0 ×1
login ×1
many-to-many ×1
message ×1
messages ×1
overflow ×1
parent-child ×1
security ×1
spring-mvc ×1
sql ×1
sql-delete ×1
updating ×1