我对JPA 2.0,Hibernate和"orphanRemoval"有疑问.
首先我的设置:
我有两个相当简单的实体类,"User"和"AvatarImage","User"有一个"AvatarImage",所以在"User"和"AvatarImage"之间有关系.
在"用户"类中,属性如下所示:
// class "User"
@OneToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY, orphanRemoval = true)
private AvatarImage avatarImage;
Run Code Online (Sandbox Code Playgroud)
这意味着,如果"avatarImage"属性设置为null,则"User"和"AvatarImage"之间的引用将被删除,"orphanRemoval"机制将从数据库中删除"avatarImage"(如果我错了,请更正我).
因此,当我为某个用户更新"avatarImage"时,我目前必须写这个:
user.setAvatarImage( null ); // First set it to null
userRepository.save( user ); // Now "orphanRemoval" will delete the old one
user.setAvatarImage( theNewAvatarImage );
userRepository.save( user );
Run Code Online (Sandbox Code Playgroud)
因此,首先将"avatarImage"属性设置为null,保存"user",然后设置新的AvatarImage"theNewAvatarImage",再次保存用户.
这是它目前适用于我的唯一方式 - "orphanRemoval"将删除旧的"avatarImage",将其设置为"null",然后保存用户.
但是,我会认为这段代码也应该有效:
user.setAvatarImage( theNewAvatarImage );
userRepository.save( user );
Run Code Online (Sandbox Code Playgroud)
所以我省略将"avatarImage"设置为"null",只是设置"theNewAvatarImage",替换旧的"avatarImage".但这不起作用,旧的AvatarImage在事务提交时不会从数据库中删除.
有谁知道,为什么第二个代码(只是替换AvatarImage而不将其设置为"null"之前)不起作用?
我非常感谢您提供的任何帮助
非常感谢!
我想使用Eclipse类路径变量来解析我的类路径中的库的附加源JAR文件.这是我目前在Elcipse(Indigo)中的".classpath"文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="lib" path="lib/ApacheCommons/commons-logging.jar"/>
<classpathentry exported="true" kind="lib" path="lib/Spring-WS/spring-ws-1.5.8-all.jar"/>
<!-- [other entries] -->
<classpathentry kind="output" path="bin"/>
</classpath>
Run Code Online (Sandbox Code Playgroud)
当我现在为"spring-ws-1.5.8-all.jar"添加源JAR文件时,".classpath"文件内容为:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="lib" path="lib/ApacheCommons/commons-logging.jar"/>
<classpathentry exported="true" kind="lib" path="lib/Spring-WS/spring-ws-1.5.8-all.jar"
sourcepath="D:/dev/sources/spring-ws-1.5.8-sources.jar"/>
<!-- [other entries] -->
<classpathentry kind="output" path="bin"/>
</classpath>
Run Code Online (Sandbox Code Playgroud)
如您所见,Eclipse使用绝对路径将"sourcepath"属性添加到"classpathentry"元素.
现在我的想法是用类路径变量"SOURCE_PATH"替换绝对路径,该变量被正确设置为"D:/ dev/sources".
(请不要问为什么我们有这个设置或建议我们必须更改它;它是一个旧项目,遗憾的是我们不能/允许更改构建结构).
我试过了
sourcepath="SOURCE_PATH/spring-ws-1.5.8-sources.jar"
Run Code Online (Sandbox Code Playgroud)
以及
sourcepath="${SOURCE_PATH}/spring-ws-1.5.8-sources.jar"
Run Code Online (Sandbox Code Playgroud)
但这两种变体都不起作用.
我似乎,我要么使用错误的语法,要么我不理解类路径变量的概念.也许classpath变量不能用于"sourcepath"属性?
非常感谢你的帮助!
我的问题是关于JPA 2.0与Hibernate,@ OneToOne关系和延迟加载.
首先我的设置:
我最近发现这样一个事实:@OneToOne关系不能以懒惰的方式(FetchType.LAZY)获取,至少没有字节代码检测,编译时间编织等.许多网站都说这个,例如:
事情是,在我的设置中,@OneToOne实体的延迟加载似乎"开箱即用",我真的想了解原因.请看看我的单元测试:
@Test
@Transactional
public void testAvatarImageLazyFetching()
{
User user = new User();
user.setAvatarImage( new AvatarImage() );
User = userRepository.save( user );
entityManager.flush();
entityManager.clear();
User loadedUser = userRepository.findOne( user.getId() );
assertNotNull( loadedUser );
PersistenceUtil persistenceUtil = Persistence.getPersistenceUtil();
assertTrue( persistenceUtil.isLoaded( loadedUser ) );
assertFalse( persistenceUtil.isLoaded( loadedUser, "avatarImage" ) );
}
Run Code Online (Sandbox Code Playgroud)
这个测试用例是成功的,在Hibernates SQL日志输出中,我可以清楚地看到,"avatarImage"不会被提取,只是"用户"(只有一个SELECT,没有JOIN,没有访问"AvatarImage"表)等等.)
User类中的单向@OneToOne关系服务如下所示:
@OneToOne( cascade = CascadeType.ALL, fetch = FetchType.LAZY )
private AvatarImage avatarImage;
Run Code Online (Sandbox Code Playgroud)
所以,一切都很简单 …
我想使用Spring WS构建一个带有JAXB的Webservice-Client,用于编组和解组Java类.
但我拥有的只是一个WSDL文件.当我理解正确时,我可以使用JAXB(xjc.exe)生成Java类,然后我可以使用Spring WS来检索Web服务数据并发送答案.
但是JAXB只能从xsd-schema文件生成Java类.我现在必须手动从WSDL文件中复制那些xsd-definitions来通过JAXB生成这些类吗?(在我的例子中,WSDL文件包含4个XSD定义).
或者有没有办法将整个WSDL转换为Java类,我可以使用它与Spring WS(我知道有"wsimport",但它是JAX-WS的一部分,我想我不能在这里使用它情景,对吗?).
那么Spring WS/JAXB /一个WSDL文件的常用方法是什么?
我真的很想使用Spring WS和JAXB - 感谢您的帮助!
我目前使用Eclipse和本地Tomcat 7服务器开发一个简单的Web应用程序.我配置了Eclipse,所以我可以从我的IDE中启动Tomcat 7 - 这里没什么了不起的.
在我的网络应用程序中,我使用SLF4J和Logback,在服务类中看起来像这样:
public class MyServiceImpl implements MyService
{
private static Logger logger = LoggerFactory.getLogger( MyServiceImpl.class );
public void doSomeStuff()
{
logger.info( "Doing some stuff" );
}
}
Run Code Online (Sandbox Code Playgroud)
我的日志记录是这样配置的:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log/MyTestWebApp.%d.log.zip</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.test" level="WARN" />
<root level="WARN">
<appender-ref ref="fileAppender" />
</root>
</configuration>
Run Code Online (Sandbox Code Playgroud)
当我启动我的Web应用程序以及本地Tomcat 7服务器时,日志输出将转到
./log/MyTestWebApp.log
Run Code Online (Sandbox Code Playgroud)
正如所料,当前目录是我的Web应用程序所在的目录(例如,我的Maven pom.xml所在的目录).
当我在远程linux机器上启动我的Web应用程序时,我找不到任何"MyTestWebApp.log"文件,不是在我的web应用程序的directoy中,也不是在Tomcat7-root目录中.
所以我的简单问题是,这些日志分别在哪里,我的"MyTestWebApp.log"文件在哪里?
非常感谢你的帮助!
我有一个关于组件扫描的问题,以及在Spring版本3.0.5.RELEASE中为同一服务类的XML配置中使用额外的bean定义.
我创建了一个带有注释的"MyService"类
@Service( "myService" )
Run Code Online (Sandbox Code Playgroud)
并且对于同一个类,有一个bean定义,如下所示:
<bean id="myService" class="....MyService" />
Run Code Online (Sandbox Code Playgroud)
我放了一个
System.out.println( "MyService has been instantiated" );
Run Code Online (Sandbox Code Playgroud)
消息到MyService类的构造函数中.当我的应用程序启动时,该消息将仅在控制台上显示一次.Spring似乎很聪明,可以检测到只有一个具有id"myService"的类的实例.
但即使我将注释更改为
@Service( "myService2" )
Run Code Online (Sandbox Code Playgroud)
并将xml bean定义id保留为"myService",该类只会被实例化一次.
我的两个问题是:
虽然这是我目前想要的行为,是否可以将XML bean定义和组件扫描混合到一个类中,让它使用不同的Bean ID实例化两次?
有谁知道,Spring的"幕后"机制是什么?Spring如何决定何时只实例化一次类?(也许不允许在同一个类中混合组件扫描和XML bean定义?)
非常感谢你的帮助!
我想将一个Java Swing组件,例如一个JButton,我也放在JFrame上,渲染到BufferedImage.这通常有效,但有一个主要缺点:文本抗锯齿,尤其是"LCD"抗锯齿模式,在渲染到BufferedImage时不起作用.
我已经将一些示例代码放在一起来演示问题,但首先是我的系统信息:
下面的示例代码将创建一个简单的JFrame,在其上放置一个JButton,然后将JButton呈现为文件"test.png":
public class TextAntiAliasingTest
{
public TextAntiAliasingTest() throws IOException
{
// Create Test-Button which will be rendered to an image
JButton button = new JButton( "The Test-Button" );
button.setSize( 200, 70 );
button.setLocation( 200, 150 );
// Create JFrame
final JFrame frame = new JFrame();
frame.setSize( 800, 600 );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setLayout( null );
frame.setLocationRelativeTo( null );
frame.add( button );
// Show JFrame
SwingUtilities.invokeLater( new Runnable() {
@Override public void …Run Code Online (Sandbox Code Playgroud) 有多种方法可以从 bash 脚本中的函数中获取结果,一种是使用引用变量,例如local -n out_ref="$1,这也是我的首选方式。
我的 bash 版本是:
GNU bash, Version 5.0.3(1)-release
Run Code Online (Sandbox Code Playgroud)
最近,我的 bash 函数之一需要生成一个关联数组作为结果,就像在这个示例代码中一样:
GNU bash, Version 5.0.3(1)-release
Run Code Online (Sandbox Code Playgroud)
我将变量声明employee为关联数组declare -A。
输出是:
employee[name]: Fry
employee[company]: Planet Express
Run Code Online (Sandbox Code Playgroud)
如果我删除该行declare -A employee,则输出为:
employee[name]: Planet Express
employee[company]: Planet Express
Run Code Online (Sandbox Code Playgroud)
有没有办法将关联数组的声明移动到函数中,这样该函数的用户就不需要事先这样做了?