我有一个用Spring MVC编写的简单的两个表应用程序,它使用Hibernate.一切都运行得很好,但如果我尝试对其中一个控制器进行单元测试,我会收到消息:
Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;
Run Code Online (Sandbox Code Playgroud)
单元测试方法如下:
@RunWith(value=SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("file:C:/NetBeansProjects/Library/build/web/WEB-INF/library- servlet.xml")
public class TestPersonController {
@Autowired
private PersonService personService;
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testGetProfile() {
Person mockPerson = new Person();
mockPerson.setPersonId(1);
mockPerson.setName("Mr Brown");
mockPerson.setAddress("Utopia Planitia on Mars");
mockPerson.setTelephone("1234567890");
mockPerson.setEmail("brown@brown.com");
when(personService.get(1)).thenReturn(mockPerson);
try {
mockMvc.perform(get("/person/profile?personId=1"))
.andExpect(status().isOk())
.andExpect(view().name("view/profile"))
.andExpect(forwardedUrl("/WEB-INF/jsp/view/profile.jsp"))
.andExpect(model().attribute("person", hasSize(1)))
.andExpect(model().attribute("person", hasItem(
allOf(
hasProperty("personId", is(1)),
hasProperty("name", is("Mr Brown")),
hasProperty("address", is("53 Onslow …Run Code Online (Sandbox Code Playgroud) 我试图在Spring MVC控制器中实现方法的单元测试,如下所示:
@Test
public void testGetProfile() {
Person mockPerson = new Person();
mockPerson.setPersonId(1);
mockPerson.setName("Mr Brown");
mockPerson.setAddress("Somewhere");
mockPerson.setTelephone("1234567890");
mockPerson.setEmail("brown@brown.com");
when(mockPersonService.get(1)).thenReturn(mockPerson);
try {
mockMvc.perform(get("/person/profile?personId=1"))
.andExpect(status().isOk())
.andExpect(view().name("view/profile"))
.andExpect(forwardedUrl("/WEB-INF/jsp/view/profile.jsp"))
.andExpect(model().attribute("person", hasSize(1L)))
.andExpect(model().attribute("person", hasItem(
allOf(
hasProperty("personId", is(1L)),
hasProperty("name", is("Mr Brown")),
hasProperty("address", is("Somewhere")),
hasProperty("telephone", is("1234567890")),
hasProperty("email", is("brown@brown.com")),
)
)));
}
catch(Exception e) {
Misc.printStackTrace(e);
}
verify(mockPersonService, times(1)).get(1);
verifyNoMoreInteractions(mockPersonService);
}
Run Code Online (Sandbox Code Playgroud)
但我得到有关消息的依赖hasSize(long)和hasProperty(...).
我在应用程序的类路径中有最新版本的Mockito,HamCrest等.
那我错过了什么?
我目前的进口是:
import library.model.Person;
import library.service.PersonService;
import library.util.Misc;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import …Run Code Online (Sandbox Code Playgroud) 当我尝试运行 Spring Boot 和 Hibernate 应用程序时,我发现它因为以下原因而崩溃:
org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing sequence [hibernate_sequence]
但我不明白为什么这是因为我没有使用 Hibernate 序列。我在 Apache Derby 中的表如下:
CREATE TABLE TEAM (
TEAM_ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
NAME VARCHAR(50) NOT NULL,
CONSTRAINT PK_TEAM PRIMARY KEY(Team_Id)
);
CREATE TABLE PLAYER (
PLAYER_ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
NAME VARCHAR(50) NOT NULL,
NUM INTEGER NOT NULL,
POSITION VARCHAR(50) NOT NULL,
TEAM_ID INTEGER,
CONSTRAINT PK_PLAYER PRIMARY KEY(PLAYER_ID), …Run Code Online (Sandbox Code Playgroud) 有谁知道为什么MYSQLDUMP只在使用以下指令运行时才执行数据库的部分备份:
"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump" databaseSchema -u root --password=rootPassword > c:\backups\daily\mySchema.dump
Run Code Online (Sandbox Code Playgroud)
有时会执行完整备份,有时备份将在仅包含数据库的一小部分后停止.这部分是可变的.
该数据库确实有几千个表,总计大约11Gb.但是这些表中的大多数都很小,只有大约1500条记录,许多只有150到200条记录.由于存储了频率数据,这些表的列数可以是数百个.
但我被告知,MySQL中模式中的表数量不是问题.正常操作期间也没有性能问题.
而使用单个表的替代方案并不可行,因为所有这些表都具有不同的列名称签名.
我应该补充说,备份期间数据库正在使用中.
在使用指令集运行备份之后:
"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump" mySchema -u root --password=xxxxxxx -v --debug-check --log-error=c:\backups\daily\mySchema_error.log > c:\backups\daily\mySchema.dump
Run Code Online (Sandbox Code Playgroud)
我明白了:
mysqldump: Couldn't execute 'SHOW TRIGGERS LIKE '\_dm\_10730\_856956\_30072013\_1375194514706\_keyword\_frequencies'': Error on delete of 'C:\Windows\TEMP\#sql67c_10_8c5.MYI' (Errcode: 13) (6)
Run Code Online (Sandbox Code Playgroud)
我认为这是一个权限问题.
我怀疑我的架构中的任何一个表都在2GB范围内.
我在具有8 Gb内存的Windows 7 64位服务器上使用MySQL Server 5.5.
有任何想法吗?
我知道改变MySQL可以打开的文件数量,open_files_limit参数,可以解决这个问题.
另一种可能性是来自此处所述的反病毒产品的干扰:
任何人都可以就一个困扰我的问题提出建议吗?我有一个Java .jar文件,它从一个方法返回一个字符串,如下所示:
integer integer integer block_of_text
Run Code Online (Sandbox Code Playgroud)
这是三个整数,可以是正数或负数,每个整数用单个空格分隔,然后是一个文本块之前的另一个空格,它可以包含任何字符,但不应包含回车符.现在我想我可以读取每个初始整数的空格字符的子字符串,然后只需一次读取剩余的文本.
我应该补充一点,无论文本块包含什么内容,都不会被分解.
但有人可以提出更好的选择吗?
感谢受访者.这让我头疼!
我正在以下网站上尝试示例应用程序:
JSF 2,PrimeFaces 3,Spring 3和Hibernate 4集成项目
但我发现在运行项目时,我得到:
严重:异常发送上下文初始化事件监听器类org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException的实例:错误创建名为"UserService"在ServletContext的资源定义[/ WEB-INF /的applicationContext豆.xml]:设置bean属性'userDAO'时无法解析bean'UserDAO'的引用; 嵌套异常是org.springframework.beans.factory.BeanCreationException:错误创建具有名称豆"的UserDAO"在ServletContext的资源定义[/WEB-INF/applicationContext.xml的]
但是,在applicationContext.xml文件中,相关代码如下:
<!-- Beans Declaration -->
<bean id="User" class="com.otv.model.User"/>
<!-- User Service Declaration -->
<bean id="UserService" class="com.otv.user.service.UserService">
<property name="userDAO" ref="UserDAO" />
</bean>
<!-- User DAO Declaration -->
<bean id="UserDAO" class="com.otv.user.dao.UserDAO">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<!-- Session Factory Declaration -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>com.otv.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
Run Code Online (Sandbox Code Playgroud)
这些类确实存在于相关的包中,并且可以在下面看到各种配置文件的位置.

我在教程和我的实现之间看到的唯一区别是我使用的是NetBeans 7.2而不是Eclipse.
谁有任何想法为什么这是?
我发现当一对多关系中的父表被更新时,子表上从属数据的外键被设置为null,从而在子表上留下孤立记录。
我有两个用Hibernate标签注释的Java类。父表是:
@Entity
@Table(name = "PERSON")
public class Person implements Serializable {
// Attributes.
@Id
@Column(name="PERSON_ID", unique=true, nullable=false)
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer personId;
@Column(name="NAME", nullable=false, length=50)
private String name;
@Column(name="ADDRESS", nullable=false, length=100)
private String address;
@Column(name="TELEPHONE", nullable=false, length=10)
private String telephone;
@Column(name="EMAIL", nullable=false, length=50)
private String email;
@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="PERSON_ID")
private List<Book> books;
Run Code Online (Sandbox Code Playgroud)
子表是:
Entity
@Table(name = "BOOK")
public class Book implements Serializable {
// Attributes.
@Id
@Column(name="BOOK_ID", unique=true, nullable=false)
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer bookId;
@Column(name="AUTHOR", nullable=false, length=50)
private String …Run Code Online (Sandbox Code Playgroud) 我有一个Spring MVC REST项目,当我尝试构建应用程序时,我收到以下消息:
NoClassDefFoundError:org/codehaus/jackson/Versioned
我相信这与应用程序中Jackson的JSON解析器的实现有关,但我正在使用:
jackson-annotations-2.4.0.jar
jackson-core-2.4.1.jar
jackson-databind-2.4.1.jar
Run Code Online (Sandbox Code Playgroud) 在我的Spring应用程序中,我有一个位于文件夹中的简单属性文件,WEB-INF\classes以便它,DispatcherServlet以及各种其他配置文件classpath.
props文件在DispatcherServletas中定义:
<bean id="propertiesFactory" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>/WEB-INF/classes/library.properties</value>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
将propertiesFactorybean注入控制器:
@Autowired
private Properties propertiesFactory;
Run Code Online (Sandbox Code Playgroud)
并在控制器的一种方法中使用:
if (adminPassword.equals(propertiesFactory.getProperty("adminPassword"))) {
Run Code Online (Sandbox Code Playgroud)
这一切都完美无缺,除了以下测试程序有以下几行:
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("library-servlet.xml");
Run Code Online (Sandbox Code Playgroud)
哪一个抛出BeanCreationException:
Injection of autowired dependencies failed
Run Code Online (Sandbox Code Playgroud)
因为:
java.io.FileNotFoundException: class path resource [WEB-INF/classes/library.properties] cannot be opened because it does not exist
Run Code Online (Sandbox Code Playgroud)
但是如果整个应用程序都可以看到props文件,为什么不用这个程序呢?
我在一个简单的Spring Boot应用程序中有一个Thymeleaf模板.该模板在表中包含一个列表,如下所示:
<p>There are <span th:text="${#lists.size(persons)}"></span> people:</p>
<table th:if="${not #lists.isEmpty(persons)}" border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Telephone</th>
<th>Email</th>
<th>Actions</th>
</tr>
<tr th:each="person : ${persons}">
<td th:text="${person.personId}"></td>
<td th:text="${person.name}"></td>
<td th:text="${person.address}"></td>
<td th:text="${person.telephone}"></td>
<td th:text="${person.email}"></td>
<td>
<a href="#" data-th-href="@{/edit(personId=${person.personId})}">Edit</a> |
<a href="#" data-th-href="@{/delete(personId=${person.personId})}">Delete</a>
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
我想根据表格中的最后一个单元格启用编辑和删除功能.但目前这两个请求都是针对HTTP GET的.这对于从服务器获取人员的详细信息以进行编辑的编辑很好,但是由于服务器上的数据更改,删除应该触发POST请求.
有没有人知道Thymeleaf是否允许每行一个表的POST请求?或者我是否必须每行编写一个简单的HTML表单?
GET表格目前是:
<td>
<a href="#" data-th-href="@{/edit(personId=${person.personId})}">Edit</a>
<!--a href="#" data-th-href="@{/delete(personId=${person.personId})}">Delete</a></td-->
<form method="get" th:action="@{/edit(personId=${person.personId})}">
<button type="submit" name="submit" value="value">Edit</button>
</form>
</td>
Run Code Online (Sandbox Code Playgroud)
我有一个链接和一个表格进行测试.
要调用的控制器方法是:
// Gets a Person.
@RequestMapping(value="/edit", method=RequestMethod.GET)
public String getEditPerson(@RequestParam("personId") String personId, Model model) { …Run Code Online (Sandbox Code Playgroud) 如果我使用Weka Explorer运行一些训练数据来反对使用线性内核的SVM测试数据,一切都很好.
但是我需要在我自己的Java中以编程方式执行此操作,并且我当前的代码如下所示:
Instances train = new Instances (...);
train.setClassIndex(train.numAttributes() - 1);
Instances test = new Instances (...) +
ClassificationType classificationType = ClassificationTypeDAO.get(6);
LibSVM libsvm = new LibSVM();
String options = (classificationType.getParameters());
String[] optionsArray = options.split(" ");
libsvm.setOptions(optionsArray);
String[] pars = libsvm.getOptions();
Evaluation eval = new Evaluation(train);
libsvm.buildClassifier(train);
eval.evaluateModel(libsvm, test);
System.out.println(eval.toSummaryString("\nResults\n======\n", false));
Run Code Online (Sandbox Code Playgroud)
但是,正在抛出异常:
eval.evaluateModel(libsvm, test);
尽管try...catch围绕此代码进行了大量尝试,但null根据下面的完整堆栈跟踪,简单地报告了异常(这确实很有用).
我不相信这个问题是由于我自己的代码,因为其他分类器已成功运行它.我正在研究这个问题的原因是环境的理论.但在哪里和什么?我正在使用Tomcat通过NetBeans 8运行我的应用程序,weka.jar并且LibSVM.jar在应用程序的.lib文件夹中有最新版本的应用程序.
但是我需要libsvm.jar从下载提供:
http://www.csie.ntu.edu.tw/~cjlin/libsvm/
如果是后一种情况,我如何解决Windows中的命名冲突,LibSVM.jar并将libsvm.jar其视为同一文件?
在过去的几个小时里,这让我很困惑.我已经尝试将两个文件LibSVM.jar和 …
我有一个名为Item的Java类型,定义如下:
private Integer itemNo;
private String itemName;
private String itemDescription;
...
Run Code Online (Sandbox Code Playgroud)
我希望能够根据itemName按降序对这种类型的arraylist进行排序.
从我读到的,这可以通过以下方式完成:
Collections.sort(items, Collections.reverseOrder());
Run Code Online (Sandbox Code Playgroud)
物品是:
ArrayList<Item> items = new ArrayList<Item>();
Run Code Online (Sandbox Code Playgroud)
但我发现对Collections.sort的调用给了我一个:
Item cannot be cast to java.lang.Comparable
Run Code Online (Sandbox Code Playgroud)
运行时异常.
有人可以建议我需要做什么吗?
我是泽西/ REST新手,我正在尝试编写一个简单的Web服务.我遇到的问题是请求之间存储数据.
Servlet可以访问会话,但我不相信Jersey/REST允许这样做.
我目前正在编写此服务,以便Android应用程序向Tomcat发出RESTful请求.
现在我还不确定这些请求的类型:它们应该是带参数的URL还是简单的XML字符串?从应用程序传输的数据类型将包括RSS提要和关键字的地址以及各种元数据.
来自服务器的XML响应将再次包括RSS提要地址,关键字,关键字的频率和其他元数据.
目前的想法是在应用程序和服务器上使用JAXB来组成XML并将其分解为Java对象.
这里没有JSON或GSON替代品.
但是如何在请求之间存储数据?如果XML形成请求和响应操作,那么将所有相关变量存储在XML中是否足够,其中每个请求将具有引用服务器数据库的ID号.
或者通过REST使用servlet的会话上下文更好?
如果上述内容含糊不清,请道歉.我是泽西岛/ REST新手.
java ×10
spring ×5
hibernate ×3
spring-mvc ×3
unit-testing ×2
android ×1
collections ×1
dependencies ×1
html ×1
http-get ×1
http-post ×1
jackson ×1
jar ×1
jersey ×1
json ×1
libsvm ×1
mysql ×1
mysqldump ×1
one-to-many ×1
rest ×1
spring-boot ×1
sql ×1
string ×1
substring ×1
thymeleaf ×1
updates ×1
weka ×1