@JoinColumn和之间的确切区别是@PrimaryKeyJoinColumn什么?
您@JoinColumn用于属于外键的列.典型的列可能看起来像(例如在具有附加属性的连接表中):
@ManyToOne
@JoinColumn(name = "...")
private OtherClass oc;
Run Code Online (Sandbox Code Playgroud)
如果我将列提升为/ PK(也就是识别关系)会发生什么?由于该列现在是PK,我必须用以下标记来标记@Id:
@Id
@ManyToOne
@JoinColumn(name = "...")
private OtherClass oc;
Run Code Online (Sandbox Code Playgroud)
现在的问题是:
和@Id+ @JoinColumn只是一样@PrimaryKeyJoinColumn吗?:
@ManyToOne
@PrimaryKeyJoinColumn(name = "...")
private OtherClass oc;
Run Code Online (Sandbox Code Playgroud)
如果没有,那@PrimaryKeyJoinColumn有什么用?
我有一组字符串,例如
my_prefix_what_ever
my_prefix_what_so_ever
my_prefix_doesnt_matter
Run Code Online (Sandbox Code Playgroud)
我只是想找到这些字符串中最长的公共部分,这里是前缀.在上面的结果应该是
my_prefix_
Run Code Online (Sandbox Code Playgroud)
字符串
my_prefix_what_ever
my_prefix_what_so_ever
my_doesnt_matter
Run Code Online (Sandbox Code Playgroud)
应该导致前缀
my_
Run Code Online (Sandbox Code Playgroud)
在Python中有一种相对无痛的方法来确定前缀(无需手动迭代每个字符)吗?
PS:我使用的是Python 2.6.3.
我们要求向用户提供两个p:calendar组件,每个组件代表一个开始和结束日期.两个日期时间都有日期,小时和分钟.PrimeFaces具有完善的mindate,maxdate,minHour,maxHour,minMinute,和minMinute属性可用.
现在的要求是:
无法将开始日期时间设置为大于或等于结束日期时间.将结束日期时间设置为小于或等于结束日期时间是不可能的.
以下等式应该成立:
begin datetime < end datetime
Run Code Online (Sandbox Code Playgroud)
现在我们尝试了以下JSF:
<p:calendar id="begin-date"
value="#{debugManager.selectedBeginDate}"
mindate="#{debugManager.minBeginDate}"
maxdate="#{debugManager.maxBeginDate}"
maxHour="#{debugManager.maxBeginHour}"
maxMinute="#{debugManager.maxBeginMinute}"
pattern="yyyy-MM-dd HH:mm"
showButtonPanel="true"
readonlyInput="true"
navigator="true"
showOn="button"
required="true">
<p:ajax event="dateSelect" update="end-date" />
</p:calendar>
<p:calendar id="end-date"
value="#{debugManager.selectedEndDate}"
mindate="#{debugManager.minEndDate}"
minHour="#{debugManager.minEndHour}"
minMinute="#{debugManager.minEndMinute}"
pattern="yyyy-MM-dd HH:mm"
showButtonPanel="true"
readonlyInput="true"
navigator="true"
showOn="button">
<p:ajax event="dateSelect" update="begin-date" />
</p:calendar>
Run Code Online (Sandbox Code Playgroud)
这是一个示例性的最小/最大方法(结束日期):
public Date getMinEndDate()
{
return this.getSelectedBeginDate();
}
Run Code Online (Sandbox Code Playgroud)
如您所见,最小结束日期是当前AJAX选择的开始日期.正确设置结束日期不允许将开始日期设置为结束日期.
当把时间纳入等式时问题就开始了......
由于p:calendar的接口有单独的方法,bean必须提供逻辑:
public int getMinEndHour()
{
Date selectedBeginDate = this.getSelectedBeginDate();
Date selectedEndDate = …Run Code Online (Sandbox Code Playgroud) 主题说明了一切......到目前为止,我认为人们没有在吸气剂和/或制定者上声明注释的优势.对我来说,这只会带来在类上传播注释的缺点,这会使类更难以理解.
将注释放在字段上会明显减少需要帮助时要发布的代码量.这只是一个很小的优势.但是对方法进行注释对我没有任何意义.
在Hibernate或其他ORM中实现复合主键时,最多有三个位置将insertable = false,updatable = false放在使用标识关系的复合主键星座中(FK是PK的一部分):
第三种是使用@IdClass和JPA 1.0 AFAIK的唯一方法.请参阅http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_Relationships.我只会考虑案例1.和2.
问:将"insertable = false,updatable = false"置于一般的首选位置是哪种方式?
我遇到过关于这个问题的Hibernate问题.例如,Hibernate 3.5.x会抱怨Zips表
CREATE TABLE Zips
(
country_code CHAR(2),
code VARCHAR(10),
PRIMARY KEY (country_code, code),
FOREIGN KEY (country_code) REFERENCES Countries (iso_code)
)
Run Code Online (Sandbox Code Playgroud)
有:
org.hibernate.MappingException: Repeated column in mapping for entity: com.kawoolutions.bbstats.model.Zip column: country_code (should be mapped with insert="false" update="false")
org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
...
Run Code Online (Sandbox Code Playgroud)
如您所见,country_code列是PK和FK.这是它的类:
实体类:
@Entity
@Table(name = "Zips")
public class Zip implements Serializable
{
@EmbeddedId
private ZipId id;
@ManyToOne …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我们进行了listType.getDescription()两次调用:
for (ListType listType: this.listTypeManager.getSelectableListTypes())
{
if (listType.getDescription() != null)
{
children.add(new SelectItem( listType.getId() , listType.getDescription()));
}
}
Run Code Online (Sandbox Code Playgroud)
我倾向于重构代码以使用单个变量:
for (ListType listType: this.listTypeManager.getSelectableListTypes())
{
String description = listType.getDescription();
if (description != null)
{
children.add(new SelectItem(listType.getId() ,description));
}
}
Run Code Online (Sandbox Code Playgroud)
我的理解是JVM以某种方式针对原始代码进行了优化,尤其是嵌套调用children.add(new SelectItem(listType.getId(), listType.getDescription()));.
比较两个选项,哪一个是首选方法,为什么?这就是内存占用,性能,可读性/易用性以及其他我现在不想到的内容.
后一个代码片段何时变得比前者更有利,也就是说,listType.getDescription()当使用临时局部变量变得更加理想时,是否存在任何(近似)调用次数,因为listType.getDescription()总是需要一些堆栈操作来存储this对象?
对JPA/Hibernate复合主键,@ IdClass或@EmbeddedId实现有什么好处?为什么?
这是一个故意天真的问题.我决定使用@EmbeddedId(无论出于何种原因),我觉得我做出了错误的选择.取消引用包含列属性的embeddedId是冗余的,并且在编码时非常容易出错.
是否有更多的理由和/或反对另一个?是JPA(规范)的推荐吗?
我总是使用以下模式构建(SLF4J)记录器:
private static final Logger log = LoggerFactory.getLogger(MyClass.class);
Run Code Online (Sandbox Code Playgroud)
到目前为止这已经奏效了,但我想知道static某些情况下的上下文以及需要一直传递具体的类文字,而不是仅仅使用非静态记录器
private final Logger log = LoggerFactory.getLogger(getClass());
Run Code Online (Sandbox Code Playgroud)
这之前基本上已经问过(并回答)了LOG4J
和这里
我意识到final基本上是强制性的,所以我想知道在非静态环境中使用SLF4J的开销实际上有多高.
问:
使用是否有任何重大的实际开销
private final Logger log = LoggerFactory.getLogger(getClass());
Run Code Online (Sandbox Code Playgroud)
过度
private static final Logger log = LoggerFactory.getLogger(MyClass.class);
Run Code Online (Sandbox Code Playgroud)
在普通(网络)应用程序?(这里不需要"讨论"高端,重载的webapps)
注意,我最终计划使用更好的方法使用CDI获得SLF4J记录器
@Inject private final Logger log;
Run Code Online (Sandbox Code Playgroud)
如http://www.seamframework.org/Weld/PortableExtensionsPackage#H-TtLoggerttInjection所述,但我需要先了解记录器缓存.
子问题:甚至可以使用?:
@Inject private static final Logger log;
Run Code Online (Sandbox Code Playgroud)
(刚开始用CDI开头)
科目说的一切.我想要的是将每个子域映射到webapp,如:
http://root.domain.com:8080 -> http://domain.com:8080/
http://manager.domain.com:8080 -> http://domain.com:8080/manager
http://abc.domain.com:8080 -> http://domain.com:8080/abc
http://def.domain.com:8080 -> http://domain.com:8080/def
Run Code Online (Sandbox Code Playgroud)
在本地主机上这将是
http://root.localhost:8080 -> http://localhost:8080/
http://manager.localhost:8080 -> http://localhost:8080/manager
http://abc.localhost:8080 -> http://localhost:8080/abc
http://def.localhost:8080 -> http://localhost:8080/def
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想使用端口80而不是8080,但这是另一个故事.我很乐意首先使用端口8080,以便URL末尾的路径消失.
注意,箭头不是重定向,但如果我按原样离开Tomcat,我会输入.
我知道Tomcat docs页面http://tomcat.apache.org/tomcat-6.0-doc/virtual-hosting-howto.html.我已多次阅读,但没有取得多大进展.我编辑了etc/hosts以添加127.0.0.1 bbstats.localhost.然后我补充道
<Host name="bbstats.localhost" appBase="webapps/bbstats"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
</Host>
Run Code Online (Sandbox Code Playgroud)
到conf目录中的Tomcat的server.xml.我的webapp的context.xml是:
<Context path="/bbstats" docBase="bbstats" debug="5" reloadable="true" crossContext="true">
</Context>
Run Code Online (Sandbox Code Playgroud)
重启Tomcat.通过Ant重新部署.进入时
HTTP://bbstats.localhost:8080 /
进入浏览器,我得到一个空白屏幕.
当使用appBase ="webapps"而不是appBase ="webapps/bbstats"时,我会看到Tomcat的root应用程序.后一种行为是预期的.但是如何让bbstats.localhost:8080转到bbstats webapp而没有URL中的尾随/ bbstats?