3

I am trying to write annotated class to fixedlength file with beanio. All classes are already annotated but I am getting exception

"Invalid field 'employees', in record 'team', in stream 'Tm': Type handler not found for type 'com.mycompany.bio.Employee'"

Below is my source code

 public static void main(String[] args) {
    StreamFactory factory = StreamFactory.newInstance();
    StreamBuilder builder = new StreamBuilder("Tm")
            .format("fixedlength")
            .parser(new FixedLengthParserBuilder())
            .addRecord(com.mycompany.bio.Team.class);
    factory.define(builder);

    Employee e1 = new Employee("EmpF1", "EmpL1", "Developer", "1", new Date());
    Employee e2 = new Employee("EmpF2", "EmpL2", "Developer", "2", new Date());
    Team team = new Team();
    team.setTeamName("Great Team");
    team.getEmployees().add(e1);
    team.getEmployees().add(e2);

    BeanWriter out = factory.createWriter("Tm", new File("C:\\Users\\user\\Desktop\\tm.dat"));

    out.write(team);
    out.flush();
    out.close();
}

Team Class:

@Record(minOccurs = 1)
public class Team {
// @Segment(collection = ArrayList.class, minOccurs = 0, maxOccurs = Integer.MAX_VALUE)  segments return single line team::teamName and 2 emp records, I want to see 3 lines 1.teamname 2 & 3 emp info
@Field(ordinal = 1, length = 106)
private List<Employee> employees;
@Field(ordinal = 0, length = 10)
private String teamName; 
.....
}

Employee Class:

@Record(minOccurs=1)
public class Employee {
@Field(ordinal = 1, length = 30)
private String firstName;
@Field(ordinal = 2, length = 30)
private String lastName;
@Field(ordinal = 3, length = 30)
private String title;
@Field(ordinal = 4, length = 8)
private String salary;
@Field(ordinal = 5, format="MMddyyyy", length = 8)
private Date hireDate;
...
}
jeton
  • 841
  • 12
  • 15

1 Answers1

5

Quick solution is:

    public static void main(String[] args) throws IOException {
    StreamFactory factory = StreamFactory.newInstance();
    StreamBuilder builderCSV = new StreamBuilder("Tm")
            .format("fixedlength")
            .parser(new FixedLengthParserBuilder())
            .addRecord(com.mycompany.bio.Team.class)
            .addRecord(com.mycompany.bio.Employee.class);
    factory.define(builderCSV);

    BeanWriter out = factory.createWriter("Tm", new File("C:\\Users\\topSecretUsername\\Desktop\\tm.txt"));

    Employee e1 = new Employee("EmpF1", "EmpL1", "Developer", "1", new Date());
    Employee e2 = new Employee("EmpF2", "EmpL2", "Developer", "2", new Date());
    Team team = new Team();
    team.setTeamName("Great Team");
    team.getEmployees().add(e1);
    team.getEmployees().add(e2);

    out.write(team);
    for (Employee e : team.getEmployees()) {
        out.write(e);
    }

    out.flush();
    out.close();

}

Team class:

@Record
public class Team {
   @Field(ordinal = 1, length = 10)
   private String teamName;
   private List<Employee> employees = new ArrayList<>();
....}

Employee:

@Record
public class Employee {

@Field(ordinal = 1, length = 30)
private String firstName;
@Field(ordinal = 2, length = 30)
private String lastName;
@Field(ordinal = 3, length = 30)
private String title;
@Field(ordinal = 4, length = 8)
private String salary;
@Field(ordinal = 5, format="MMddyyyy", length = 8)
private Date hireDate;
....}

Output file content:

Great Team
EmpF1                         EmpL1                         Developer 1           20150622
EmpF2                         EmpL2                         Developer 2           20150622
jeton
  • 841
  • 12
  • 15
  • Excellent example. Work like a charm. – jalpa Sep 07 '18 at 10:56
  • How does it solve the problem of marshalling/unmarshalling a container bean if one now needs to know the internal shape of that bean and traverse all of its constituents elements and contained collections manually in order to write the passed in containing bean contents to the output stream? Why can't one have a `@Field` annotation on a collection as per your error? Why isn't there a `@Group` annotation in BeanIO framework? – Simeon Leyzerzon Jul 15 '23 at 03:37