title

fire chief's random developer tidbits

Wednesday, July 31, 2013

Jaxb name conflict with underscores

<xs:element name="_SortFields" nillable="true" type="tns:_SortFields"/>

will conflict with

<xs:element name="SortFields" nillable="true" type="tns:SortFields"/>

giving the error:

Two declarations cause a collision in the ObjectFactory class.
  line 300 of file:blah.xsd

(Related to above error) This is the other declaration.   
  line 385 of file:blah.xsd

because it is removing underscores from names by default.

A bindings file like this will fix it:
<jxb:bindings version="2.1" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
              xmlns:jaxws="http://java.sun.com/xml/ns/jaxws" 
              xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
              xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jxb:globalBindings underscoreBinding="asCharInWord">
        <xjc:simple/>
        <xjc:serializable />
    </jxb:globalBindings>
</jxb:bindings>

Thursday, July 25, 2013

Spring security default https port 8443 and Glassfish

Glassfish and Spring Security have different default ports for https traffic 8181 vs 8443.  To get that working right in a development environment I did the following:

pom.xml:
    <properties>
        <http.port>8080</http.port>
        <http.port>8181</http.port>
    </properties>

    <build>
        <resources>
             <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </reources>
...

       <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webResources>
                        <resource>
                            <filtering>true</filtering>
                            <directory>src/main/resources/WEB-INF/context</directory>
                            <targetPath>WEB-INF/context</targetPath>
                            <includes>
                                <include>applicationContext.xml</include>
                                <include>applicationContext-security.xml</include>
                                <include>datasourceContext.xml</include>
                            </includes>
                        </resource>
...
                


    <profiles>
        <profile>
            <id>production</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <http.port>80</http.port>
                <https.port>443</https.port>
            </properties>
        </profile>


applicationContext-security.xml:
    <http>
        <intercept-url pattern="/" access="permitAll" requires-channel="https"/>
        <intercept-url pattern="/**" access="hasRole('USER')" requires-channel="https" />
        <port-mappings>
            <port-mapping http="${http.port}" https="${https.port}"/>
        </port-mappings>
    </http>

Part of the answer came from here about moving context files to resources though I am using Netbeans, so I didn't have to worry about m2e problems he had.

Wednesday, July 24, 2013

Deleting child entity when the child is removed from the parent collection.

orphanRemoval on JPA relationships is very useful, and possibly misnamed.  It doesn't handle orphan removal, but it handles removing deleted child entities meaning that in JPA 2.0 you can remove an object in a collection and then persist the parent and it will actually remove the deleted child entity from the database.

http://docs.oracle.com/javaee/6/api/javax/persistence/OneToMany.html#orphanRemoval

I think the name orphanRemoval is strange.  It's a terrible analogy, but when you have a parent duck and its ducklings, if you remove a duckling (kill it) it doesn't become an orphan, it just becomes dead.