-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upgrade jpa, a start on attributeconverter
- Loading branch information
Showing
5 changed files
with
185 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/com/dieselpoint/norm/converter/IntArrayToListConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.dieselpoint.norm.converter; | ||
|
||
import java.sql.Array; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.persistence.AttributeConverter; | ||
import javax.persistence.Converter; | ||
|
||
import com.dieselpoint.norm.DbException; | ||
|
||
@Converter | ||
public class IntArrayToListConverter implements AttributeConverter<List<Integer>, java.sql.Array> { | ||
|
||
@Override | ||
public Array convertToDatabaseColumn(List<Integer> attribute) { | ||
return new SimpleArray(java.sql.Types.INTEGER, attribute.toArray()); | ||
} | ||
|
||
@Override | ||
public List<Integer> convertToEntityAttribute(Array dbData) { | ||
|
||
try { | ||
if (dbData.getBaseType() != java.sql.Types.INTEGER) { | ||
throw new DbException("Database is not returning an integer array"); | ||
} | ||
int[] arr = (int[]) dbData.getArray(); | ||
List<Integer> out = new ArrayList<>(); | ||
for (int i = 0; i < arr.length; i++) { | ||
out.add(arr[i]); | ||
} | ||
return out; | ||
|
||
} catch (SQLException e) { | ||
throw new DbException(e); | ||
} | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/com/dieselpoint/norm/converter/SimpleArray.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.dieselpoint.norm.converter; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.Map; | ||
|
||
public class SimpleArray implements java.sql.Array { | ||
|
||
private int baseType; | ||
private Object [] arr; | ||
|
||
public SimpleArray(int baseType, Object [] arr) { | ||
this.baseType = baseType; | ||
this.arr = arr; | ||
} | ||
|
||
@Override | ||
public String getBaseTypeName() throws SQLException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public int getBaseType() throws SQLException { | ||
return baseType; | ||
} | ||
|
||
@Override | ||
public Object getArray() throws SQLException { | ||
return arr; | ||
} | ||
|
||
@Override | ||
public Object getArray(Map<String, Class<?>> map) throws SQLException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Object getArray(long index, int count) throws SQLException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Object getArray(long index, int count, Map<String, Class<?>> map) throws SQLException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public ResultSet getResultSet() throws SQLException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public ResultSet getResultSet(Map<String, Class<?>> map) throws SQLException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public ResultSet getResultSet(long index, int count) throws SQLException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public ResultSet getResultSet(long index, int count, Map<String, Class<?>> map) throws SQLException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void free() throws SQLException { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.