Collection required when maxOccurs is greater than 1 and class is set

1,239 views
Skip to first unread message

Renato Souza

unread,
Oct 21, 2014, 2:28:41 PM10/21/14
to bea...@googlegroups.com
Hello. I'm using the 2.1.0 version of beanio and I need to map one file whose structure is the following:

HEADER
 
 n RECORDS
 
 TRAILER


I've annotated everything and I'm building the objects using StreamBuilder. Here is the code:

        GroupBuilder groupBuilder = new GroupBuilder("file")
            .type(PaymentSlipFile.class)
            .addRecord(PaymentSlipHeader.class)
            .addRecord(PaymentSlipRecord.class)
            .addRecord(PaymentSlipTrailer.class);
       
        StreamBuilder streamBuilder = new StreamBuilder("paymentslip")
            .format("fixedlength")
            .parser(new FixedLengthParserBuilder().recordTerminator("\r\n"))
            .addGroup(groupBuilder);

        factory.define(streamBuilder);
        BeanReader in = factory.createReader("paymentslip", new FileReader(file));
        PaymentSlipFile psFile = (PaymentSlipFile) in.read();
        in.close();


And here are my annotated classes:

@Group(order = 1, name = "file")
public class PaymentSlipFile {

    private PaymentSlipHeader header;
    private List<PaymentSlipRecord> records = new ArrayList<PaymentSlipRecord>();
    private PaymentSlipTrailer trailer;

}

@Record(name="header", order = 1, minOccurs = 1, maxOccurs = 1)
public class PaymentSlipHeader {

    @Field(at = 0, length = 1, rid = true, literal = "0")
    private Integer headerCode;

}

@Record(name = "records", order = 2, minOccurs = 1, maxOccurs = -1, collection = PaymentSlipRecord.class)
public class PaymentSlipRecord {

    @Field(at = 0, length = 1, rid = true, literal = "1")
    private Integer recordType;

}

@Record(name = "trailer", order = 3, minOccurs = 1, maxOccurs = 1)
public class PaymentSlipTrailer {

    @Field(at = 0, length = 1, rid = true, literal = "9")
    private Integer recordType;

}
 

As always I'm having trouble mapping the 'n' records thing.
Everything I try gives the same error:
org.beanio.BeanIOConfigurationException: Invalid record 'records', in group 'file', in stream 'paymentslip': Collection required when maxOccurs is greater than 1 and class is set
Caused by: org.beanio.BeanIOConfigurationException: Collection required when maxOccurs is greater than 1 and class is set


If I change the List<PaymentSlipRecord> to a single PaymentSlipRecord object and set its maxOccurs to 1, then it works.
But I really need the "n records" working.
Any tips?

ps.: of course i cut some code to simplify

Kevin

unread,
Oct 22, 2014, 10:51:45 PM10/22/14
to bea...@googlegroups.com
Hello,

I think you just need to move the @Record annotations to the fields in the PaymentSlipFile class, and then your GroupBuilder becomes:

GroupBuilder groupBuilder = new GroupBuilder("file")
            .type(PaymentSlipFile.class);

i.e. no need to explicitly add the records.

Thanks,
Kevin 

Renato Souza

unread,
Oct 23, 2014, 7:37:12 AM10/23/14
to bea...@googlegroups.com
If I do so, I get the following error:

Caused by: org.beanio.BeanIOConfigurationException: At least one record or group is required.

And my group code is:

@Group(order = 1, name = "file", xmlType = XmlType.NONE)
public class PaymentSlipFile {


    @Record(name="header", order = 1, minOccurs = 1, maxOccurs = 1)
    private PaymentSlipHeader header;
    @Record(name = "records", order = 2, minOccurs = 1, maxOccurs = -1, collection = List.class)
    @Fields({
        @Field(name = "recordType", at = 0, length = 1, rid = true, literal="1")
    })

    private List<PaymentSlipRecord> records = new ArrayList<PaymentSlipRecord>();
    @Record(name = "trailer", order = 3, minOccurs = 1, maxOccurs = 1)
    private PaymentSlipTrailer trailer;

}

Manreet Chahal

unread,
Mar 24, 2016, 12:14:39 PM3/24/16
to beanio-users
I am getting the exact same issue. Did you figure out how to correct this one? There is very little documentation and examples on how to use beanio with annotations.

Renato Souza

unread,
Mar 24, 2016, 1:44:54 PM3/24/16
to bea...@googlegroups.com
Dude, I fixed this issue, but I don't remember how anymore.. it was a long time ago.
But I looked in my old codes here and found this particular one.
My @Group file has exactly the same code as my last post.
Maybe the problem was on the repeatable @Record.. so here is it's mapping:
@Record(name = "records", order = 2, minOccurs = 1, maxOccurs = -1, collection = List.class)
@Fields({
 @Field(name = "id", at=0, length = 1, rid = true, literal="1")
})

And here is the mapping of the field representing the ID of this @Record:
@Field(at = 0, length = 1, rid = true, literal = "1") 
private Integer id; 

I believe the problem was with this rid = true (it was not declared before).
Good luck.

--
You received this message because you are subscribed to a topic in the Google Groups "beanio-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beanio/hG2ELd38_EQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beanio+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Manreet Chahal

unread,
Mar 24, 2016, 3:07:31 PM3/24/16
to beanio-users

I have put the rid = true but it still gives me the same issue. Did you also use a mapping file? I am not using a mapping file and hoping annotations should be sufficient.

Renato Souza

unread,
Mar 24, 2016, 5:29:34 PM3/24/16
to bea...@googlegroups.com
no mapping xml.. annotations only
Message has been deleted

Bob Dixon

unread,
Aug 17, 2016, 1:47:47 PM8/17/16
to beanio-users
ritz, did you ever resolve your issue?
I'm receiving a similar error.  I seem to be having trouble with Grouping the different records.
org.beanio.BeanIOConfigurationException: Invalid group 'file', in stream 'cbinput': At least one record or group is required.
 
My annotated definitions are very in the same order and format as Renato Souza.  

Renato Souza

unread,
Aug 17, 2016, 2:09:07 PM8/17/16
to bea...@googlegroups.com
Does your entities have any property that identifies the entity uniquely? Something like an @Id of JPA?
If so, try to set rid = true on its @Field.
My solution was something like this.
Hope it helps.

--
You received this message because you are subscribed to a topic in the Google Groups "beanio-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beanio/hG2ELd38_EQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beanio+unsubscribe@googlegroups.com.

OttoDelphia

unread,
Aug 23, 2016, 1:00:20 PM8/23/16
to beanio-users
Thank you for responding.
Yes, I included the rid=true on my annotated fields for each record.  
I'm sure there is some simple configuration that I have incorrect.  I'm a bit new to this sort of programming so I could easily be missing something.

I've posted more detail of my issue on the following topic.  "Having trouble defining the grouping for multiple annotated record types using StreamFactory."  https://groups.google.com/forum/?fromgroups#!topic/beanio/pMIsmiPdF5E

To unsubscribe from this group and all its topics, send an email to beanio+un...@googlegroups.com.
Message has been deleted

Kabelo Laka

unread,
Sep 28, 2018, 9:25:24 AM9/28/18
to beanio-users
Hi Otto

Did you find a solution to this issue?

Kabelo Laka

unread,
Sep 28, 2018, 9:26:59 AM9/28/18
to beanio-users
Hi Renato

Did you find a solution to this problem?
Reply all
Reply to author
Forward
0 new messages