JiBX – java.util.Date – Marshalling and UnMarshalling (TimeZone)

In XML schema we cannot specify the timezone for date field. In XML message we can populate time with or without the timezone component.

JiBX is smart to take care of the timezone component when marshalling and unmarshalling date type field that mapped to java.util.Date.

Lets say i have field or attribute like messageDate=”2001-12-17T09:30:47″ in XML message. Since there is not timezone specified Jibx assumes its in UTC. My server is running in CST, So when JiBX converts the request in java util data object. Its populated like this Mon Dec 17 03:30:47 CST 2001. The CST is -6:00 from UTC. So this is correct. There is not need to do any manual conversion. Some may consider this as defect but this is not.

Lets  says the same attribute is sent in XML as messageDate=”2001-12-17T09:30:47-06:00″. This time XML attribute has the timezone component. When JiBX unmarshall it convert it to Date object Mon Dec 17 09:30:47 CST 2001. For may this easy to understand then example above.

JIbx when marshall \ serialize java.util.Date object to XML, it convert it to date (in whichever timezone it is) in UTC. As per XML standard add Z to add of it

So in first case Mon Dec 17 03:30:47 CST 2001 get serializes to messageDate=”2001-12-17T09:30:47Z”

In second case this Mon Dec 17 09:30:47 CST 2001 get serializes to messageDate=”2001-12-17T15:30:47Z” 

In both case we had time in CST timezone (6 hours behind UTC) when it got serialized it converted to UTC so 6 horus are added. 

More details on other date type – look at JiBX http://jibx.sourceforge.net/binding/date-time.html

Advertisements

A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance

A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance: com.sample1.hibernate.User.address; nested exception is org.hibernate.HibernateException: A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance: com.sample1.hibernate.User.address

You probably see this error in one to many relationship in hibernate.

Lets say we have one to many relationship between the 2 entity.

User has one or more address.

User is defined as –>

public void setAddress(List<Address> address) { this.address = address; }

@Cascade( {CascadeType.ALL, CascadeType.DELETE_ORPHAN})
@OneToMany(mappedBy = “user”, fetch = FetchType.LAZY)
@JoinColumn(name=”user_id”)
public List<Address> getAddress() { return address; }

Address is defined as –>

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = “user_id”)
public User getUser() { return user; }

public void setUser(User user)  { this.user = user; }

Use case is we want to delete all the existing addresses for the user and add a new set of addresses to the user. So wrote below code to achieve this.

User user = loadUserByLogin(“shri”);

// remove all the existing addresses.
user.getAddress().clear();

// add new address
Address newAddress = new Address();
newAddress.setIndex1(6L);
newAddress.setUser(user);
newAddress.setAddress(“test”);
saveAddress(newAddress);

List<Address> addresses = new ArrayList<Address>();
addresses.add(newAddress);
user.setAddress(addresses);
saveUser(user);

When the transaction commits, you see said error in the log. There couple of ways you will be able to fix this –>

1. Instead of attaching new address list to the user, Add the addresses to existing address list that are already associated with user.

// remove all the existing addresses.
user.getAddress().clear();

// add new address
Address newAddress = new Address();
//newAddress.setId(2L); // required for oracle //ORA-01400: cannot insert NULL into (“SPATEL”.”ADDRESS”.”ID”)
newAddress.setIndex1(8L);
newAddress.setUser(user);
newAddress.setAddress(“test”);
saveAddress(newAddress);

List<Address> addresses = new ArrayList<Address>();
addresses.add(newAddress);
user.getAddress().addAll(addresses);
saveUser(user);

2. Link the address to user, but do not linking from user to address. Initial I thought this was wierd, but if you read way hibernate internal works, this will make sense.

// remove all the existing addresses.
user.getAddress().clear();

// add new address
Address newAddress = new Address();
newAddress.setIndex1(7L);
newAddress.setUser(user);
newAddress.setAddress(“test”);
saveAddress(newAddress);

saveUser(user);

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: Extended key usage does not permit use for TLS client authentication

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: Extended key usage does not permit use for TLS client authentication

I have service exposed on 2 way ssl port on my server. When a new client was tryign to consume the service we were seeing above error in our server log. We doubled checked, we had client cert in our server trust store. Also our client our server cert in their trust store. We enable ssl debug parameter  -Djavax.net.debug=all, to ssl handshake details.

In SSL handshake details, i saw following happens as part of handshake->

1.    Client hello.

2.    Server hello.

a.    Server send it identity cert.

b.    Challenges client to send its cert by issuing “CertificateRequest”. We see Client CA’s in this certificate request.

3.    Client provides the certificate chain.

4.    The server fails to validate that cert with following error

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: Extended key usage does not permit use for TLS client authentication

Client certificate looks something like ..

chain [0] = [

[

Version: V3

Subject: CN=XXXX, OU=XXXXXXXXX, O=XXXX, C=US

.

.

.

.

.

[5]: ObjectId: 2.5.29.37 Criticality=false

ExtendedKeyUsages [

  serverAuth

]

So as you see Client cert is issue by the Extended key usage is only set to allow this for serverAuth. We had client change there certificate to include usage as clientAuth.

Following possible usage for cert from open SSL documentatin.

serverAuth             SSL/TLS Web Server Authentication.
 clientAuth             SSL/TLS Web Client Authentication.
 codeSigning            Code signing.
 emailProtection        E-mail Protection (S/MIME).
 timeStamping           Trusted Timestamping
 msCodeInd              Microsoft Individual Code Signing (authenticode)
 msCodeCom              Microsoft Commercial Code Signing (authenticode)
 msCTLSign              Microsoft Trust List Signing
 msSGC                  Microsoft Server Gated Crypto
 msEFS                  Microsoft Encrypted File System
 nsSGC                  Netscape Server Gated Crypto