楼主: Lisrelchen
1413 3

Java EE 6 Cookbook for Securing, Tuning, and Extending Enterprise Applications [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2015-4-25 19:51:54 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

Java EE 6 Cookbook for

Securing, Tuning, and Extending Enterprise Applications


Book Description
Secure your Java applications using Java EE built-in features as well as the well-known Spring Security framework. Utilize related recipes for testing various Java EE technologies including JPA, EJB, JSF, and Web services. Explore various ways to extend a Java EE environment with the use of additional dynamic languages as well as frameworks. A learn-by-example based approach that focuses on key concepts to provide the foundation to solve real world problems.

Book Details
  • Publisher:        Packt Publishing
  • By:        Mick Knutson
  • ISBN:        978-1-8496-8316-6
  • Year:        2012
  • Pages:        356
  • Language:        English
  • File size:        29.2 MB
  • File format:        PDF
  • Download:        

    本帖隐藏的内容

    Java EE 6 Cookbook for Securing, Tuning, and Extending Enterprise Applications.rar (11.36 MB, 需要: 20 个论坛币) 本附件包括:
    • Java EE 6 Cookbook for Securing, Tuning, and Extending Enterprise Applications.pdf




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Applications Application Enterprise Extending Cookbook additional framework including concepts features

本帖被以下文库推荐

沙发
mike68097(未真实交易用户) 发表于 2015-4-26 04:09:30

藤椅
Lisrelchen(未真实交易用户) 发表于 2016-11-28 00:55:09

Understanding @CollectionTable

  1. Getting ready

  2. In order to use the @CollectionTable annotation, you first need to understand why they have been added to the JPA specification. While I feel this is a powerful new feature, it is important not to overuse this feature for mapping.

  3. How to do it...

  4. The @CollectionTable annotation can be added to a Collection<T> or a Map<K, V> entity attribute. This allows for some interesting mappings to store related entities.

  5. In the case of a Collection<V>, you can choose a String or Integer as your collection element such as Collection<String>. You might also choose to have a custom collection of Objects such as List<Phone>. You might even find a usage for implementing a Map<K, V> Collection table. In these cases, you will still use a String or Integer as a key, and a custom object as your value such as Map<String, Address> to define your custom mapping:

  6. To start with a simple example, we can start by mapping a collection of Strings to our Entity:
  7. @ElementCollection
  8. @CollectionTable(name = Constants.HOBBIES,
  9. joinColumns = @JoinColumn(name = Constants.CUSTOMER_ID))
  10. @Column(name = Constants.HOBBY_NAME, nullable = true)
  11. private Collection<String> hobbies = new HashSet<String>();
  12. We need to annotate this collection with @ElementCollection to denote that this is a basic attribute type, and that it is not acting as a container. We then define @CollectTable annotation where we can specify the table name used for the hobbies and the respective joinColumn, or foreignKey referring back to our customer. When we generate a <dataset> for testing this mapping, we end up with the following code:
  13. <CUSTOMER id='103' USERNAME="user3"
  14. FIRSTNAME="Foo" LASTNAME="UserTwo"/>
  15. <HOBBIES CUST_ID="102" HOBBY_NAME="BASE-Jumping"/>
  16. The same mapping will work when trying to map a collection of custom objects such as List<Phone>. Our @JoinColumn still allows JPA to map the Phone back to the Customer to whom this phone belongs. When we generate a <dataset> for testing this mapping, we end up with the following code:
  17. <CUSTOMER id='103' USERNAME="user3"
  18. FIRSTNAME="Foo" LASTNAME="UserTwo"/>
  19. <HOBBIES CUST_ID="102" HOBBY_NAME="BASE-Jumping"/>
  20. <PHONES AREACODE="415" PHONE_NUMBER="5551212"
  21. TYPE="WORK" CUST_ID="102"/>
  22. When we start getting a little more complex by mapping a Map<K,V> into our Entity, we need to be more specific on how JPA will map our custom object.
  23. In this case, we still define our @ElementCollection, and our @CollectionTable just as mapping a collection, List, or Set:

  24. @ElementCollection
  25. @CollectionTable(name = Constants.CUSTOMER_ADDRESSES,
  26. joinColumns = @JoinColumn(name =
  27. Constants.CUSTOMER_ID))
  28. @MapKeyColumn(name = Constants.ADDRESS_KEY)
  29. private Map<String, Address> addresses =
  30. new HashMap<String, Address>();
  31. We now add an additional annotation, @MapKeyColumn, to denote the key in our Address that we want to map back to our Customer. When we look at the <dataset> used for testing this Entity, we can see that our ADDRESS_KEY and CUST_ID are merged into one table:
  32. <CUST_ADDRESSES ADDRESS_KEY="Primary" CITY="Exton"
  33. POSTCODE=""
  34. PROVINCE="91335"
  35. STATE="PA" STREET="555 Boot Road"
  36. STREET2="b101" TYPE="RESIDENTIAL"
  37. CUST_ID="102"/>
  38. The ADDRESS_KEY denotes the Key used in the Map<K,V>, for our Address Object. Then we have a reference CUST_ID back to our Customer Table.
复制代码

板凳
Lisrelchen(未真实交易用户) 发表于 2016-11-28 00:57:18

Auditing previous JPA Operations

  1. Getting ready

  2. Building upon the first recipe, we start with a customer, and we want to audit all CRUD operations that occur. We can achieve this in two basic ways:

  3. Using @PrePersist and @PreUpdate annotations
  4. Implement a JPA Lifecycle Change Listener
  5. Using @PrePersist and @PreUpdate are simple and easy to implement. However, they are only going to allow for one revision to be saved. This might suffice for minimal auditing:

  6. public static ThreadLocal currentUser = new ThreadLocal();
  7. @Column(name = Constants.AUDIT_USER)
  8. protected String auditUser;
  9. @Temporal(TemporalType.TIMESTAMP)
  10. @Column(name = Constants.AUDIT_TIMESTAMP)
  11. protected Calendar auditTimestamp;
  12. We need to add a property to our entity for holding the user that made the last update, and when the last update occurred. This gives us the managed properties we need in order to add our audit information.

  13. How to do it...

  14. To implement a single audit record implementation, we can simply add a service method to our Entity, which is annotated with @PrePersist or @PreUpdate respectively to be initiated during the respective lifecycle:

  15. @PrePersist
  16. @PreUpdate
  17. public void updateAuditInfo() {
  18. setAuditUser((String) currentUser.get());
  19. setAuditTimestamp(Calendar.getInstance());
  20. }
  21. Usually you would create a super class that contains the properties you want to share with children. In this case, we have an AuditableEntity that is extended by customer. In this recipe, we have created an AuditableEntity that our customer extends.
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-22 22:12