我有一个客户表,每个客户都属于一个公司,公司希望他们的客户编号从0开始并随着他们添加客户而增加,当公司B添加客户时,公司A的客户编号不应受到影响.CustomerId内部可以是任意数字,customerNumber必须在companyId的上下文中无间隙递增(无间隙我的意思是0,1,2,3,4,如果2被删除,它已经消失,下一个插入应该是5而不是2)
Example:
companyId customerNumber customerId
0 0 0
0 1 1
0 2 2
0 3 3
0 4 4
0 5 5
1 0 6
1 1 7
1 2 8
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更好的方法来做它而不是打开一个事务,找到max customerNumber,使用max + 1作为customerNumber插入一个条目并关闭事务
我可以使用某种注释来指定生成customerNumber的条件吗?下一个customerNumber应该是该公司内可用的最高数量.(我有大约20个基于日期和comapnyId具有类似的人类可读增量id要求的其他实体,我想尽可能使customerNumber类型字段生成为万无一失,我不想记得每次我坚持一个新实体时这样做)
就像是:
@SequenceGenerator(uniqueOver="companyId,customerNumber",strategy=GenerationType.AUTO)
private Long customerNumber;
Run Code Online (Sandbox Code Playgroud)
由于我正在使用股票和财务数据,因此该解决方案应符合ACID标准.
更新:我已将id重命名为customerId,将customerId重命名为customerNumber以防止混淆.
更新:当我的意思是无间隙时,我的意思是customerNumbers应该是0,1,2,3,4,5,6,7,8,9 - 如果我删除了数字4它将永远消失并且下一个插入应该是10除非我创建一家新公司,否则他们的第一个客户应该从0开始.
更新:我正在使用spring with hibernate,所以@PrePersist注释对我不起作用.如果@PrePersist被建议作为解决方案,那么它需要在spring下工作,因此需要回答Simon的问题:在Spring中启用@PrePersist和@PreUpdate
建议的答案,我不确定:
@Entity
@Table(name = "Customer")
public class Customer {
@Table(name = "CustomerNumber")
@Entity(name = "sequenceIdentifier")
public static class CustomerNumberSequenceIdentifier {
@Id
@GenericGenerator(name = "sequence", strategy = …Run Code Online (Sandbox Code Playgroud) 我有点困惑为什么ngAfterContentInit在这种情况下执行两次.我已经创建了我们的应用程序的精简版本来重现该错误.简而言之,我使用a *contentItem来标记组件,然后由standard-layout组件拾取以进行渲染.只要我遵循这个模式,demo的ngAfterContentInit执行两次.
我将演示应用程序放在github上,这将重现错误:https: //github.com/jVaaS/stackoverflow/tree/master/ngaftercontentinit
否则这里是重要的一点:
越野车,app.dart:
@Component(
selector: "buggy-app",
template: """
<standard-layout>
<demo *contentItem></demo>
</standard-layout>
""",
directives: const [
ContentItemDirective,
StandardLayout,
Demo
]
)
class BuggyApp implements AfterContentInit {
@override
ngAfterContentInit() {
print(">>> ngAfterContentInit: BuggyApp");
}
}
Run Code Online (Sandbox Code Playgroud)
标准layout.dart:
////////////////////////////////////////////////////////////////////////////////
///
/// Standard Layout Component
/// <standard-layout></standard-layout>
///
@Component(
selector: "standard-layout",
template: """
<div *ngFor="let item of contentItems ?? []">
<template [ngTemplateOutlet]="item.template"></template>
</div>
""",
directives: const [ROUTER_DIRECTIVES, ContentItem])
class StandardLayout implements AfterContentInit …Run Code Online (Sandbox Code Playgroud) 在发出SOAP请求并且验证失败时,我得到的响应是:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Client</faultcode>
<faultstring xml:lang="en">Validation error</faultstring>
<detail>
<spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">xyz-pattern-valid: Value '.....' is not facet-valid with respect to pattern '[0-9x]+' for type 'ABC'.</spring-ws:ValidationError>
</detail>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Run Code Online (Sandbox Code Playgroud)
有没有办法摆脱各地的spring-ws显然暴露了底层SOAP服务使用的是Spring和Java?
我已经看过扩展PayloadValidatingInterceptor,但是看不到明显的方法来摆脱spring-ws标签.
我正在尝试在具有单独配置的maven插件中包含两个执行块,但是maven似乎忽略了执行块内部的配置块,而仅使用执行块外部的配置块。
因此,为了缩小问题的范围,我打开了一个有效的插件部分,将工作配置稍微移到了执行块的内部,然后停止了工作(wsdl仍然被拾取(实际上两个都被拾取)),但这很简单因为它位于默认目录中,所以当配置部分位于执行块内部时,将拾取绑定文件和其他配置中的非配置文件,实际上,它甚至不应该知道第二个wsdl,因为我没有在任何地方指定它):
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>wsdla</id>
<phase>generate-sources</phase>
<configuration>
<packageName>com.mycee.project.model</packageName>
<sourceDestDir>src/main/java</sourceDestDir>
<wsdlFiles>
<wsdlFile>
${basedir}/src/wsdl/a.wsdl
</wsdlFile>
</wsdlFiles>
<bindingDirectory>
${basedir}/src/wsdl/binding
</bindingDirectory>
<verbose>true</verbose>
</configuration>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
Run Code Online (Sandbox Code Playgroud)
这是我需要解决的Maven问题,还是下颌问题,我该如何解决?
如果将版本更改为2.3,则会出现以下错误:
[错误]无法在项目mycee-project上执行目标org.jvnet.jax-ws-commons:jaxws-maven-plugin:2.3:wsimport(default-cli):目标org.jvnet.jax-ws的执行default-cli -commons:jaxws-maven-plugin:2.3:wsimport失败:字符串索引超出范围:-1-> [帮助1]
运行mvn clean jaxws:wsimport -X我可以在调试输出中看到它正在使用默认值:
[DEBUG] -----------------------------------------------------------------------
[DEBUG] Goal: org.jvnet.jax-ws-commons:jaxws-maven-plugin:2.3:wsimport (default-cli)
[DEBUG] Style: Regular
[DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
<bindingDirectory default-value="${basedir}/src/jaxws"/>
<destDir default-value="${project.build.outputDirectory}"/>
<encoding>${project.build.sourceEncoding}</encoding>
<extension default-value="false"/>
<genJWS default-value="false"/>
<implDestDir default-value="${project.build.sourceDirectory}"/>
<keep default-value="true"/>
<localRepository default-value="${localRepository}"/>
<pluginArtifactMap>${plugin.artifactMap}</pluginArtifactMap>
<quiet default-value="false"/>
<remoteRepositories default-value="${project.pluginArtifactRepositories}"/>
<settings>${settings}</settings> …Run Code Online (Sandbox Code Playgroud) 我在我的春季项目中第一次使用Camunda BPMN2并尝试了解我的一些事情......
在我的applicationContext中,我有以下块来设置Camunda:
<!-- Setup BPMN Process Engine -->
<bean id="processEngineConfiguration" class="org.camunda.bpm.engine.spring.SpringProcessEngineConfiguration">
<property name="processEngineName" value="engine" />
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="true" />
<property name="jobExecutorActivate" value="false" />
<property name="deploymentResources" value="classpath*:*.bpmn" />
</bean>
<bean id="processEngine" class="org.camunda.bpm.engine.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
<context:annotation-config />
Run Code Online (Sandbox Code Playgroud)
我已经设置了两项服务:
@Component(value="service1")
public class Service1 implements JavaDelegate { …Run Code Online (Sandbox Code Playgroud) 我需要限制对包含会话令牌的 cookie 的访问,以便 javascript 无法访问它。给出的建议是在 cookie 上设置 Secure 和 HttpOnly 标志。
我在使用@ResponseBody 时遇到了没有设置 cookie 的问题,所以我在 HandlerInterceptor 中设置了 cookie。
public class COOKIEFilter implements org.springframework.web.servlet.HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
Cookie cookie = new Cookie("timestamp", new Long(new Date().getTime()).toString());
cookie.setSecure(true);
// how do I set the http-only flag?
httpServletResponse.addCookie(cookie);
return true;
}
Run Code Online (Sandbox Code Playgroud)
如chrome控制台所示,设置了Secure,但没有设置HTTP

我已经尝试在 servlet 3.0 sepcification 下向 web.xml 添加参数,允许在会话 cookie 上设置安全和 http-only,但由于我需要自己处理会话(Spring MVC 应用程序需要保持无状态),所以赢了不适合我。
更新:
我正在使用 Tomcat7,目前使用 Servlet 2.5 和 Spring 3.2.8。
我正在尝试使用HQL删除实体,但失败了。
TypedQuery<Seller> query = Seller.entityManager().createQuery(
"DELETE FROM Seller AS o WHERE o.company=:company AND o.id=:id", Seller.class);
query.setParameter("company", company);
query.setParameter("id", id);
int result = query.executeUpdate();
Run Code Online (Sandbox Code Playgroud)
我得到的stacktrace:
Update/delete queries cannot be typed; nested exception is java.lang.IllegalArgumentException: Update/delete queries cannot be typed
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:296)
at org.springframework.orm.jpa.aspectj.JpaExceptionTranslatorAspect.ajc$afterThrowing$org_springframework_orm_jpa_aspectj_JpaExceptionTranslatorAspect$1$18a1ac9(JpaExceptionTranslatorAspect.aj:33)
at com.ahp.core.model.Seller.deleteSeller_aroundBody4(Seller.java:111)
at com.ahp.core.model.Seller.deleteSeller(Seller.java:1)
at com.ahp.core.processor.SellerProcessor.delete(SellerProcessor.java:175)
at com.ahp.core.processor.SellerProcessor.consume(SellerProcessor.java:80)
at com.ahp.core.processor.SellerProcessor.consume(SellerProcessor.java:1)
at com.ahp.messaging.processor.AbstractRPCConsumer.onMessage(AbstractRPCConsumer.java:32)
at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:228)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:756)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:679)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:82)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:167)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1241)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:660)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:1005)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:989)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$700(SimpleMessageListenerContainer.java:82)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1103)
at java.lang.Thread.run(Thread.java:744)
Caused by: …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以在继续处理之前等待回调。
我正在使用一个在内部处理 future 的库,如果成功,则进行回调,否则在内部处理错误而不进行回调。
现在我尝试使用这个库创建一个实例,然后用随机测试数据填充它,然后更新该实体。
Map generateRandomizedInstance() {
lib.createEntity((result1){
result1["a"] = generateRandomA();
result1["b"] = generateRandomB();
result1["c"] = generateRandomC();
...
lib.updateEntity(result1, (result2){
// want to return this result2
return result2;
})
});
}
Run Code Online (Sandbox Code Playgroud)
如果我只创建一个实体并更新一次,那就没问题了,但我想创建大量随机数据:
ButtonElement b = querySelector("button.create")..onClick.listen((e){
for (int i = 0; i < 500; i++) {
generateRandomizedInstance();
}
});
Run Code Online (Sandbox Code Playgroud)
由于回调返回的速度不够快,这段代码很快就会崩溃。
我尝试将方法签名更改为
generateRandomizedInstance() async {
Run Code Online (Sandbox Code Playgroud)
然后做:
for (int i = 0; i < 500; i++) {
print(await generateRandomizedInstance());
}
Run Code Online (Sandbox Code Playgroud)
但该等待语法似乎无效,并且我不完全确定如何在某种将来包装该回调代码,以便我可以等待回调返回,然后再继续循环的下一次迭代。
我在末尾尝试了一个 while 循环,generateRandomizedInstance等待结果变量不为空,但这会杀死浏览器,并且看到我并不总是得到回调,在某些情况下它可能会导致无限循环。
关于如何在等待回调时暂停 for 循环的任何想法/建议?
是否可以使用 Pebble 模板引擎从字符串构建模板而不必提供文件名?
val engine = PebbleEngine.Builder().build()
val writer = StringWriter();
engine.getTemplate("test.html").evaluate(writer);
Run Code Online (Sandbox Code Playgroud)
test.html例如,我将如何提供以下格式的模板,而不是提供?
val template = "Hello {{world}} - {{count}} - {{tf}}"
Run Code Online (Sandbox Code Playgroud)
我目前在 Pebble 2.2.1
<!-- Pebble -->
<dependency>
<groupId>com.mitchellbosecke</groupId>
<artifactId>pebble</artifactId>
<version>2.2.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
基于我收到的答案的解决方案:
val context = HashMap<String, Any>()
...
val engine = PebbleEngine.Builder().loader(StringLoader()).build();
val writer = StringWriter();
engine.getTemplate(template).evaluate(writer, context);
println(writer.toString());
Run Code Online (Sandbox Code Playgroud) 我正在尝试访问Angular2 Dart项目中的DOM,因为我使用的是不兼容Angular2的GUI库.
从按钮单击事件调用时,以下代码100%工作:
Modal.use();
Modal carriersModal = new Modal(querySelector("div.select-carrier-modal"));
carriersModal.show();
Run Code Online (Sandbox Code Playgroud)
我现在想要访问相同的DOM,但将其打包到组件中:
@Component(
selector: 'select-carrier',
templateUrl: '../templates/select-carrier-component.html'
)
class SelectCarrierComponent implements AfterContentInit {
ElementRef _el;
SelectCarrierComponent(ElementRef el){
this._el = el;
}
@override
ngAfterContentInit( ) {
Modal.use();
Modal carriersModal = new Modal(this._el.nativeElement.querySelector("div.select-carrier-modal"));
carriersModal.show();
}
}
Run Code Online (Sandbox Code Playgroud)
在angular2组件内,print(this._el.nativeElement);打印出来select-carrier.如果我这样做this._el.nativeElement.querySelector,结果就是null我放在那里.如果我打电话this._el.nativeElement.children,我会得到一个空列表.
奇怪的是,new Modal(querySelector("div.select-carrier-modal")).show();当从按钮点击通过另一个组件调用时,调用仍然有效,但我无法从组件内部开始工作.
@override
ngAfterContentInit( ) {
print(querySelector("div.select-carrier-modal"));
print((this._el.nativeElement as Element).innerHtml);
print((this._el.nativeElement as Element).children);
print((this._el.nativeElement as Element).nodes);
Run Code Online (Sandbox Code Playgroud)
只需打印出来
null
<!--template bindings={}-->
[]
[template bindings={}]
Run Code Online (Sandbox Code Playgroud)
知道如何从组件内部访问DOM以便我可以连接Modal吗?